第三讲 动手动脑-1

观察输出,总结。
class Grandparent {
    public Grandparent() {
        System.out.println("GrandParent Created");
    }
    public Grandparent(String string) {
        
        System.out.println("GrandParent Crented.String:"+string);
    }
}

class Parent extends Grandparent {
    public Parent() {
        //super("hello.Grandparent");
        System.out.println("Parent Created");
        //super("hello.Grandparent.");
    }

}

class Child extends Parent{
    public Child() {
        System.out.println("Child Created");
    }

}

public class Testlnherits {
    public static void main(String args[]) {
        Child c=new Child();
    }

}
输出结果:
GrandParent Created Parent Created Child Created

结论:通过super调用基类构造方法,必须是子类构造方法中的第一个语句。

猜你喜欢

转载自www.cnblogs.com/chenyuchun/p/9904680.html