【发布时间】:2016-06-22 13:53:51
【问题描述】:
在这个例子中,我有一个带有空白构造函数的子类。在超类中提供了两个构造函数。当我在下面的代码中执行 main 时,结果显示它打印“我是没有参数的超级构造函数”。 我的问题是,为什么编译器会忽略子类中的空白构造函数?如果我能接受这种“无知”,那么我理解编译器将执行子类的默认构造函数,在这种情况下,它是超类中没有参数的构造函数。 这是子类:
package extendisevil;
public class SubConstructorInherit extends ConstructorInherit {
public SubConstructorInherit(String param) {
}
public static void main(String[] args) {
SubConstructorInherit obj = new SubConstructorInherit("valami");
}
}
这里是超级:
package extendisevil;
public class ConstructorInherit {
public ConstructorInherit(String name) {
System.out.println("I am the super constructor with String parameter: " + name);
}
public ConstructorInherit() {
System.out.println("I am the super constructor without parameter.");
}
}
感谢您的帮助!
【问题讨论】:
标签: java inheritance constructor