为什么 superclass 构造函数必须用 super() 而不是用它的 class 名称调用?
Why superclass constructor must be called with super() and not with its class name?
对于初学者来说,规则很简单。 子能先于父存在吗? No. 这就是为什么我们在创建子类的时候总是调用super()
。这样,超类的每个成员都会“复制”到子类。
所以我举个例子:
class Duck extends Animal {
int size;
public Duck(int size) {
super();
this.size = size;
}
}
我的问题是为什么我们不能用 Animal()
而不是 super()
调用 Animal 构造函数。或者,为什么我们不能这样做:
class Duck extends Animal {
int size;
public Duck(int size) {
Animal();
this.size = size;
}
}
两个代码都不应该编译吗?我的意思是它们在技术上没有区别,编译器几乎可以通过关键字 extendsDuck
IS-A Animal
知道 Duck
IS-A Animal
=30=]。那到底是什么问题呢?
Why superclass constructor must be called with super() and not with its class name?
简单。这是因为 Java 语法就是这样设计的。
假设,他们可以设计Java语法,这样你也可以1使用超类的名称而不是 super
。但他们没有。故事结束,真的。 (设计决策是在 1995 年之前做出的,现在更改它们已经太晚了。)
备案,相关语法在JLS 8.8.7.1中指定,语法产生式为ExplicitConstructorInvocation
.
1 - 我们不知道他们为什么选择 super
而不是其他可能性的详细信息。那是很久以前的事了,会议是私密的。然而,作为一般规则,提供两种方式来用一种编程语言说同一件事并没有取得任何实际意义。事实上,它可能只会使语言更难阅读......和学习。
一种好的编程语言可以让作者表达他们的意图,尽可能少的语法变化来实现这一点。变化越多,程序员脑子里的规则就越多,支持所有这些变化的编译器也就越复杂。
假设我避开 Java 命名约定并在静态方法的名称中使用大写字母:
class Duck extends Animal {
int size;
public Duck(int size) {
Animal(); //super or static method?
this.size = size;
}
private static void Animal() {
System.out.println("I'm a static method");
}
}
Duck构造函数调用的是Animal构造函数还是静态方法?我们可以引入一个涵盖这种情况的新规则,但它会引入更多的复杂性。
您所建议的是实现完全相同的事情的两种方法。当 super
独自完美地实现该目的时,没有理由支持这一点。
为什么他们不允许我们使用 parent()
或 superconstructor()
或 superclass()
? 你 有责任提供一个令人信服的理由,说明为什么应该 支持它。
That is why we always call super() when creating a subclass.
我认为答案就在问题中:
因为你正在创建一个子类。
My question is why can't we call Animal constructor with Animal() and not super().
在您的子类中,您无权访问“Animal()”构造函数。
如果您想详细了解您的困境:https://www.w3schools.com/java/java_inheritance.asp
对于初学者来说,规则很简单。 子能先于父存在吗? No. 这就是为什么我们在创建子类的时候总是调用super()
。这样,超类的每个成员都会“复制”到子类。
所以我举个例子:
class Duck extends Animal {
int size;
public Duck(int size) {
super();
this.size = size;
}
}
我的问题是为什么我们不能用 Animal()
而不是 super()
调用 Animal 构造函数。或者,为什么我们不能这样做:
class Duck extends Animal {
int size;
public Duck(int size) {
Animal();
this.size = size;
}
}
两个代码都不应该编译吗?我的意思是它们在技术上没有区别,编译器几乎可以通过关键字 extendsDuck
IS-A Animal
知道 Duck
IS-A Animal
=30=]。那到底是什么问题呢?
Why superclass constructor must be called with super() and not with its class name?
简单。这是因为 Java 语法就是这样设计的。
假设,他们可以设计Java语法,这样你也可以1使用超类的名称而不是 super
。但他们没有。故事结束,真的。 (设计决策是在 1995 年之前做出的,现在更改它们已经太晚了。)
备案,相关语法在JLS 8.8.7.1中指定,语法产生式为ExplicitConstructorInvocation
.
1 - 我们不知道他们为什么选择 super
而不是其他可能性的详细信息。那是很久以前的事了,会议是私密的。然而,作为一般规则,提供两种方式来用一种编程语言说同一件事并没有取得任何实际意义。事实上,它可能只会使语言更难阅读......和学习。
一种好的编程语言可以让作者表达他们的意图,尽可能少的语法变化来实现这一点。变化越多,程序员脑子里的规则就越多,支持所有这些变化的编译器也就越复杂。
假设我避开 Java 命名约定并在静态方法的名称中使用大写字母:
class Duck extends Animal {
int size;
public Duck(int size) {
Animal(); //super or static method?
this.size = size;
}
private static void Animal() {
System.out.println("I'm a static method");
}
}
Duck构造函数调用的是Animal构造函数还是静态方法?我们可以引入一个涵盖这种情况的新规则,但它会引入更多的复杂性。
您所建议的是实现完全相同的事情的两种方法。当 super
独自完美地实现该目的时,没有理由支持这一点。
为什么他们不允许我们使用 parent()
或 superconstructor()
或 superclass()
? 你 有责任提供一个令人信服的理由,说明为什么应该 支持它。
That is why we always call super() when creating a subclass.
我认为答案就在问题中: 因为你正在创建一个子类。
My question is why can't we call Animal constructor with Animal() and not super().
在您的子类中,您无权访问“Animal()”构造函数。
如果您想详细了解您的困境:https://www.w3schools.com/java/java_inheritance.asp