package TestInherits;
//继承问题及super的用法
class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } public Grandparent(String string) { System.out.println("GrandParent Created.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 TestInherits { public static void main(String args[]) { Child c = new Child(); } }

1.未取消注释时的结果:

 

2.将第一个注释取消时的输出结果:

3.将第二个注释取消,程序会报错:

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

子类的构造方法在运行之前,必须调用父类的构造方法

原因:构造函数的主要作用是初始化。

不可变类:final

 final即最完美的,最终的,不可更改。

奇怪的输出:

package text;

public class text {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(new A());
    }

}
package text;

public class A {

}

输出结果:

探索步骤:

 

 

 posted on 2018-11-08 13:10  Aurinko  阅读(123)  评论(0编辑  收藏  举报