Java 为什么构造函数不被继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18415488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why Constructors are not inherited?
提问by Raghu Kariganur
I have been learning constructors in Inheritance using Eclipse Juno.
我一直在使用 Eclipse Juno 学习继承中的构造函数。
When I press ctrl+O twice in the childClass, It shows inherited members. But I happen to see even the Constructor of super class in the inherited members
当我在 childClass 中按 ctrl+O 两次时,它显示继承的成员。但是我碰巧在继承的成员中甚至看到了超类的构造函数
But it is said that constructors are not inherited...
但是据说构造函数不是继承的...
Can someone please explain this behaviour?
有人可以解释这种行为吗?
回答by Chris Jester-Young
Constructors are chained: each constructor you write must eventually invoke one of the superclass constructors. Example:
构造函数是链式的:您编写的每个构造函数最终都必须调用超类构造函数之一。例子:
public class MyException extends RuntimeException {
public MyException(String message) {
super(message); // invokes RuntimeException(String) constructor
}
}
A super(...)
or this(...)
constructor invocation, if any, must appear as the first statement in your constructor. If neither of those is specified, super()
is implicitly assumed, which will chain up to the superclass's default constructor. (And if the superclass has no default constructor, then the compilation will fail.)
一个super(...)
或this(...)
构造函数调用,如果有的话,必须出现在构造函数的第一个语句。如果这些都没有指定,super()
则隐式假定,它将链接到超类的默认构造函数。(如果超类没有默认构造函数,那么编译就会失败。)
回答by Suresh Atta
Unlike fields, methods, and nested classes ,Constructors are not class members.
与字段、方法和嵌套类不同,构造函数不是类成员。
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
子类从其超类继承所有成员(字段、方法和嵌套类)。构造函数不是成员,因此子类不会继承它们,但是可以从子类调用超类的构造函数。
回答by Juned Ahsan
Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
构造函数不是成员,因此子类不会继承它们,但是可以从子类调用超类的构造函数。
Source: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
来源:http: //docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Eclipse method help using Ctrl+O shows what all methods that you can call from the current class. Hence parent constructors are also displayed in that as you can call it using super
.
使用 Ctrl+O 的 Eclipse 方法帮助显示了您可以从当前类调用的所有方法。因此,父构造函数也显示在其中,因为您可以使用super
.
回答by Theodoros Chatzigiannakis
Only members are inherited, and a constructor is not considered a member.
只有成员被继承,构造函数不被视为成员。
To understand whyconstructors are not inherited, consider that inheritance in OOP is part of the mechanism that allows an object to be treatedas another, more general object. This requires that all data members and all methods are inherited.
要理解为什么构造函数不被继承,请考虑 OOP 中的继承是允许将对象视为另一个更通用的对象的机制的一部分。这要求继承所有数据成员和所有方法。
What inheritance is notintended to do, is allow one object to be instantiatedin the same manner as another, more general object.
继承不打算做的是允许一个对象以与另一个更通用的对象相同的方式实例化。
This is because a constructor mustinitialize an object to a valid stateand what's enough information to initialize valid state for a superclass object might not be enough information to initialize valid state for the subclass object!
这是因为构造函数必须将对象初始化为有效状态,而足以为超类对象初始化有效状态的信息可能不足以为子类对象初始化有效状态!
To work around this, if constructors were inherited, when you extended a class from a library you'd have to manually opt out of the constructors you don't want inherited. This is cumbersome and error prone, as when a new version of that library comes out with more constructors in that base class, your own class is now subject to invalid initializations (through the leaked constructors), unless you release an update too. Or it could be that adding a constructor in your own superclass will "break" your own subclasses and you'd have to go to each subclass and opt out of the new ones. In other words, the validity of your code would be more tightly coupledto the base you've used.
为了解决这个问题,如果构造函数是继承的,当你从库中扩展一个类时,你必须手动选择退出你不想继承的构造函数。这既麻烦又容易出错,因为当该库的新版本在该基类中带有更多构造函数时,您自己的类现在会受到无效初始化(通过泄露的构造函数)的影响,除非您也发布更新。或者可能是在您自己的超类中添加构造函数会“破坏”您自己的子类,您必须转到每个子类并选择退出新的子类。换句话说,您的代码的有效性将与您使用的基础更加紧密地耦合。
On the other hand, "opting in", by defining your own constructors explicitly and chaining them with those of the base class makes more sense practically and is safer for the validity of your component. This opting in is done by chaining, the process of invoking a base class constructor at the beginning of another constructor.
另一方面,通过显式定义自己的构造函数并将它们与基类的构造函数链接起来,“选择加入”在实践中更有意义,并且对组件的有效性更安全。这种选择是通过链接完成的,链接是在另一个构造函数的开头调用基类构造函数的过程。
Now Eclipse (which I don't use, so I'm basing this on what you're describing in your question) may probably be listing the constructors available for use in chaining because looking for them is a very common scenario (unless you're invoking a very simple or parameterless constructor). In other words, the constructors are listed in the inherited members for convenience but, as we've said, strictly speaking, they are not inherited.
现在 Eclipse(我不使用它,所以我基于你在你的问题中描述的内容)可能会列出可用于链接的构造函数,因为寻找它们是一个非常常见的场景(除非你重新调用一个非常简单或无参数的构造函数)。换句话说,为方便起见,构造函数列在继承成员中,但正如我们所说,严格来说,它们不是继承的。