我一直认为构造函数不是继承的,但是看看这段代码:
class Parent {
Parent() {
System.out.println("S1");
}
}
class Child extends Parent {
Child() {
System.out.println("S2");
}
}
public class Test5 {
public static void main(String[] args) {
Child child = new Child();
}
}
//RESULT:
//S1
//S2
它表明 Child 继承了构造函数。为什么结果上有 S1?是否有可能创建 2 个没有参数的构造函数,并且在没有基本构造函数的结果上只有子构造函数(只有 S2)?