java中的Person、Student和GoodStudent类的继承

这期内容当中小编将会给大家带来有关java中的Person、Student和GoodStudent类的继承,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联是一家专注于成都网站建设、成都做网站与策划设计,丰宁网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:丰宁等地区。丰宁做网站价格咨询:028-86922220

有这样三个类,Person,Student,GoodStudent。其中Student继承了Person,GoodStudent继承了Student,三个类中只有默认的构造函数,用什么样的方法证明在创建Student类的对象的时候是否调用了Person的构造函数,在创建GoodStudent类的对象的时候是否调用了Student构造函数?如果在创建Student对象的时候没有调用Person的构造函数(我也不知道什么情况下不会去调用,如果都是默认无参构造函数的话),那么采用什么样的手段可以调用父类的构造函数?

一、需要分析

1、Person,Student,GoodStudent三个类的继承关系
2、实现三个class的构造函数
3、打印信息查看各个类的构造函数是否被调用

二、技术点

1、弄清楚Java 类的无参构造函数是默认调用的
2、如果父类的构造函数是有参的,那么要在子类的构造函数的第一行加入super(args); 来确认对哪个父类构造函数的调用

代码:

package com.itheima;

/**
 * 9、
 * 有这样三个类,Person,Student.GoodStudent。其中Student继承了Person,GoodStudent继承了Student,
 * 三个类中只有默认的构造函数,用什么样的方法证明在创建Student类的对象的时候是否调用了Person的构造函数,
 * 在创建GoodStudent类的对象的时候是否调用了Student构造函数?如果在创建Student对象的时候没有调用Person的构造函数
 * ,那么采用什么样的手段可以调用父类的构造函数?
 * 
 * @author 281167413@qq.com
 */

public class Test9 {

	public static void main(String[] args) {
		Student s1 = new Student();
		System.out.println("-------------------------------");
		Student s2 = new Student();
		System.out.println("-------------------------------");
		GoodStudent g1 = new GoodStudent();
		System.out.println("-------------------------------");
	}

}

class Person {

	Person() {
		System.out.println("I'm Person!");
	}

	Person(String arg) {
		System.out.println(arg);
	}

	Person(String arg1, String arg2) {
		System.out.println(arg1 + arg2);
	}
}

class Student extends Person {

	Student() {
		super("have arg!"); //
		System.out.println("I'm Student!");
	}

	Student(String arg) {
		super("have arg!", "in Person");
		System.out.println(arg);
	}
}

class GoodStudent extends Student {

	GoodStudent() {
		super("from GoodStudent!");
		System.out.println("I'm GoodStudent!");
	}

}

打印构造函数的调用过程:

have arg!
I'm Student!
-------------------------------
have arg!
I'm Student!
-------------------------------
have arg!in Person
from GoodStudent!
I'm GoodStudent!
-------------------------------

上述就是小编为大家分享的java中的Person、Student和GoodStudent类的继承了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。

当前名称:java中的Person、Student和GoodStudent类的继承
地址分享:https://www.cdcxhl.com/article22/pgdecc.html

成都网站建设公司_创新互联,为您提供关键词优化Google小程序开发网站设计网站设计公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设