黑马程序员技术交流社区
标题:
求解答?????
[打印本页]
作者:
纯情的小VV
时间:
2015-1-9 16:25
标题:
求解答?????
有这样三个类,Person、Student、GoodStudent。
其中GoodStudent继承于Student,Student继承于Person。
如何证明创建GoodStudent时是否调用了Person的构造函数?
在GoodStudent中是否能指定调用Student的哪个构造函数?
在GoodStudent中是否能指定调用Person的哪个构造函数?
作者:
Afridoce
时间:
2015-1-9 17:02
class Test9
{
public static void main(String[] args)
{
GoodStudent goodstu3 = new GoodStudent("goodstudent","指定调用");
}
}
class Person
{
Person()
{
//若子类调用到该构造函数则打印提示
System.out.println("调用Person的构造函数--1");
}
Person(String str1)
{
System.out.println(str1+"调用Person的构造函数--2");
}
Person(String str1,String str2)
{
System.out.println(str1+str2+"调用Person的构造函数--3");
}
}
class Student extends Person
{
Student(String str1)
{
System.out.println(str1+"Student的构造函数--1");
}
Student(String str1,String str2)
{
//若子类调用到该构造函数则打印提示
System.out.println(str1+str2+"Student的构造函数--2");
}
}
class GoodStudent extends Student
{
GoodStudent(String str1,String str2)
{
//调用父类构造函数
super(str1,str2);
}
}
作者:
AtheerCHA
时间:
2015-1-9 17:22
在Person顶层父类构造函数中写一个输出语句“我来自Person父类”,student中的构造函数 中写上“我来自student父类”。在goodstudent创建对象初始化时就会显示这两条来自父类的语句了。表名调用了父类的构造方法。
作者:
AtheerCHA
时间:
2015-1-9 17:24
在Peson中的构造函数中写上“我来自person”,在Student中的构造函数中中写上“我来自student”,在goodstudent创建对象初始化时,就会显示这两条语句表名调用了,这两个构造方法。
作者:
jojo
时间:
2015-1-9 17:29
第一:在三个类的构造函数中分别增加一条打印输出语句,就可以判断哪个构造函数被调用了。
第二:在GoodStudent的构造函数的第一行通过super来指定调用Student的哪个构造函数
第三:GoodStudent不能直接调用Person的构造函数,但是Student可以调用Person的构造函数,构造函数的调用只能向上调用高一级的,不能跨越多级。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2