Java - 为什么重写方法被调用两次(或者至少它看起来像这样)?

Oma*_*r D 5 java

下面有这个输出.

Hello World!
main.ConstructedDerivedClass:6.0
main.ConstructedDerivedClass:6.0
Run Code Online (Sandbox Code Playgroud)
public class ConstructedDerivedClass extends ConstructedBase {

    private static final double version = 6.0;

    public static void main(String[] args) {

        System.out.println("Hello World!");

        ConstructedDerivedClass derivedClass = new ConstructedDerivedClass();
    }

    public ConstructedDerivedClass() {
        showMyAttributes();
    }

    @Override
    protected void showMyAttributes() {
        System.out.println(this.getClass().getName() + ":" + version);
    }  
}

public class ConstructedBase {

    private static final double version = 15.0;

    public ConstructedBase() {
        showMyAttributes();
    }

    protected void showMyAttributes() {
        System.out.println(this.getClass().getName() + ":" + version);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望它只显示一行,即子类的类(ConstructedDerivedClass).但它反而打印两次.我知道一般你应该避免从构造函数中调用overriden方法,但我想亲眼看看它是如何工作的.

实际上,我知道为什么版本在两行上都是'6.0' - 因为字段被声明为静态,当然静态字段首先被初始化.但仍然不明白为什么两行.

任何指导将不胜感激.

vkg*_*vkg 7

这是因为你写的时候

public ConstructedDerivedClass() {
    showMyAttributes();
}
Run Code Online (Sandbox Code Playgroud)

编译器实际上在字节代码中调用超级默认构造函数,因此它等效于

public ConstructedDerivedClass() {
    super();        
    showMyAttributes();
} 
Run Code Online (Sandbox Code Playgroud)


Kon*_*Kon 6

实例化对象时,它会implicit, no-args在运行自己的构造函数之前对其父类进行超级构造函数调用.

你可以想象在你的构造函数中总是存在这一行:

public MyClass() {
    super(); //If this line is not here, it is implicit.
    //rest of the code
}
Run Code Online (Sandbox Code Playgroud)

您可以通过提供自己的explicit, any-args超级构造函数调用来覆盖它,但它必须是方法的第一行.

public MyClass() {
    super(1, "Hello Word", null); //written explicitly, no other super-constructor code will run
    //rest of the code
}
Run Code Online (Sandbox Code Playgroud)

当超类没有定义no-args构造函数时,这可能很有用.