A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 980595778 中级黑马   /  2015-5-13 18:12  /  653 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

基础测试中的一道题
有这样三个类,Person、Student、GoodStudent。 其中GoodStudent继承于Student,Student继承于Person。如何证明创建GoodStudent时是否调用了Person的构造函数?在GoodStudent中是否能指定调用Student的哪个构造函数? 在GoodStudent中是否能指定调用Person的哪个构造函数?
大神们看看这样写对不对?

public class Test9 {
        public static void main(String[] args) {
                GoodStudent goodStudent = new GoodStudent();
        }
}

class Person {
        // 创建一个人类
        Person() {
                System.out.println("Person 无参构造器");
        }

        Person(String arg) {
                System.out.println("Person 有参构造器");
        }

}

// 创建一个学生类;继承于人类
class Student extends Person {
        Student() {
                super("继承 Person"); //
                System.out.println("Student 无参构造器");
        }

        Student1(String arg) {
                super("继承 Person");
                System.out.println("Student 有参构造器");
        }
}

// 创建一个goodstudent类继承于学生类;
class GoodStudent extends Student {
        GoodStudent() {
                super("继承 Student");
                System.out.println("我是 GoodStudent");
        }

}


3 个回复

倒序浏览
你这都显示的写出了调用父类的构造函数了,怎么还能证明呢。
你直接把super去掉,实例化的子类对象的时候,父类的无参构造函数会默认被调用,你的输出结果会有你的无参构造函数里的语句
回复 使用道具 举报
看看~看看~~
回复 使用道具 举报
把super去掉,然后来看打印经过,你这个就能知道了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马