本帖最后由 xingfeichen 于 2015-5-5 22:37 编辑
package com.itheima;
/*
有这样三个类,Person、Student、GoodStudent。
其中GoodStudent继承于Student,Student继承于Person。
如何证明创建GoodStudent时是否调用了Person的构造函数?
在GoodStudent中是否能指定调用Student的哪个构造函数?
-->可以
在GoodStudent中是否能指定调用Person的哪个构造函数?
* */
/*
* -->在Person类的构造函数中设定一个输出语句,
-->当创建号GoodStudent对象后,看是否输出语句被执行
-->通过以下实验,该方法可行
* 如下程序运行结果为
* Person is running
Stu is running
goodstu is running
说明在创建GoodStudent时是否调用了Person的构造函数
在GoodStudent中无法指定调用Person的构造函数,
都是默认第一个函数构造函数
* */
class Person{
Person(){
System.out.println("Person is running");
}
Person(int number){
System.out.println("the numbei is::"+number);
}
}
class Students extends Person{
Students(){
System.out.println("Stu is running");
}
}
class GoodStudent extends Students{
GoodStudent(){
System.out.println("goodstu is running");
}
GoodStudent(int num){
System.out.println("goodstu is running"+num);
}
}
public class Test9 {
public static void main(String[] args) {
GoodStudent goodstu = new GoodStudent();
GoodStudent goodstu1 = new GoodStudent(1);
// goodstu.setIfo("zhangsan", 30);
}
}
PS:上体是我昨天做的基础测题,虽然证明了问题,但是还是有好多问题是我没弄明白的
1, 我如果想调用Person类中的第二个构造方法,在创建goodStuden类是应该怎么做?
2,我如果想要使用这样的一个继承体系,怎么才能合理的初始化,也就是在创建对象时尽量少的占用内存资源?
比如说name,age,sex等的初始化
|
|