作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们开始:
public class Parent {
public Parent(String name) {
this.name = name;
}
public String name = null;
}
public class Child extends Parent {
public Child(String name) {
super(name); // If I comment this : Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor
}
}
据说“如果您不自己调用 super 构造函数,编译器将为您插入对 super() 的无参数调用,作为构造函数中的第一个语句。”
最佳答案
Is this true for the argument-ed constructors as well?
是的。
Mustn't the compiler call the argument-ed super constructor with the same signature of the child constructor?
没有。如果您想要调用不同的基类构造函数,则必须自己调用它,并根据需要传递参数。
Why it has been designed like this?
因为您提出的替代方案(自动转发所有参数)一般来说不是很有用。
关于java - 编译器在 java 子类的有参构造函数中调用无参数 super(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509074/
我是一名优秀的程序员,十分优秀!