0
public class A {
  public A(){
    System.out.println("A created");        
  }
  public static void main(String[] args) {
    new B();
  }
}

class B extends A{
    public B(){
        System.out.println("B created");
    }       
}

上述程序的输出将是

A created

B created

我不明白构造函数 A() 是如何被调用的。B() 中没有调用超级。但是仍然调用了 A() 。

4

2 回答 2

7

When class B extends class A, it will call constructor A( ) by default.

That's the reason why the program prints A created before B created.

于 2013-03-19T19:30:13.373 回答
2

In child classes, super() is automatically called implicitly to ensure the object is properly constructed.

于 2013-03-19T19:31:05.777 回答