在超类和基类的构造函数中调用层次结构

nr5*_*nr5 3 java inheritance constructor

class a
{
a(){System.out.println("A");}
}

class b extends a
{
b()
{
super();
System.out.println("B");}
}

class c extends b
{
c(){System.out.println("c");}
}

class last
{
public static void main(String aaa[])
{
c obj = new c();
}
}
Run Code Online (Sandbox Code Playgroud)

输出来自:

一个

C

不应该是:

一个

一个

C

因为超级关键字

Ami*_*nde 8

super();如果你没有明确指定,它总是在那里.如果您没有明确指定,Java只会添加自动调用.

所以你的代码

    B() {
        super();
        System.out.println("B");
    }
Run Code Online (Sandbox Code Playgroud)

和...一样

    B() {
        System.out.println("B");
    }
Run Code Online (Sandbox Code Playgroud)