子类是否从其超类继承构造函数?
在子类中,我们可以使用子类的构造函数来初始化数据成员,该构造函数在内部调用超类的构造函数super()
。如果子类无法从其超类继承构造函数,那么 super()
调用如何初始化超类?
In a subclass we can initialize data members using the subclass's constructor which internally calls the superclass's constructor super()
. If a subclass can't inherit constructors from its superclass then how can the super()
call initialize the superclass?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
子类的构造函数可以调用超类的构造函数,但它们不会被继承。
需要明确的是,这意味着如果您有类似以下内容:
那么您不能编写:
因为没有
Sub(int)
构造函数。将构造函数视为具有正在初始化的对象的隐式参数的非继承静态方法可能会有所帮助。
来自 Java 语言规范第 8.8 节:
A constructor from a subclass can call constructors from the superclass, but they're not inherited as such.
To be clear, that means if you have something like:
then you can't write:
because there's no
Sub(int)
constructor.It may be helpful to think of constructors as uninherited static methods with an implicit parameter of the object being initialized.
From the Java Language Spec, section 8.8:
子类不能继承其超类的构造函数。
构造函数是类的特殊函数成员,因为它们不被子类继承。构造函数用于在创建时为对象提供有效状态。
主要原因之一是因为您可能不想覆盖超类构造函数,如果它们是继承的,则这是可能的。通过让开发人员能够重写超类构造函数,您将削弱该语言的封装能力。
另请参阅:构造函数永远不会被继承
No a subclass cannot inherit the constructors of its superclass.
Constructors are special function members of a class in that they are not inherited by the subclass. Constructors are used to give a valid state for an object at creation.
One of the main reasons is because you probably don't want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.
See also : Constructors are never inherited
super
是 Java 关键字引用超类,super()
是调用超类构造函数的方法。构造函数不是继承的,但您仍然可以调用它。super
is a Java keyword for referring to the superclass, andsuper()
is the way to call your superclass's constructor. The constructor is not inherited but you are still able to call it.不..根本不会...子类不会继承其超类构造函数..相反,它可以使用关键字“super()”调用其超类构造函数...
No..Not at all never...A Subclass Does not inherits its Superclass constructor..instead it call it can call its Superclass Constructor Using keyword "super()"...
首先,构造函数不是类的成员,只有成员是被继承的。
其次,我们可以想象一些情况,我们不希望子类具有与父类相同的构造函数。
想象一个带有构造函数 Vehicle(intwheels) 的抽象类 Vehicle 和一个子类 Bicycle。
根据定义,自行车有 2 个轮子,因此我们可以想象 Bicycle 构造函数将调用 super(2) ,在这种情况下 Bicycle 不公开构造函数 Bicycle(intwheels) 不是更好吗?
First of all, constructors are not members of classes, and only members are inherited.
Second, we can imagine cases for which we don't want subclasses to have the same constructors than the parent class.
Imagine an abstract class Vehicle with a constructor Vehicle(int wheels), and a subclass Bicycle.
By definition, a Bicycle has 2 wheels so we can imagine that Bicycle constructor will call super(2) and isn't it better in this case that Bicycle does not expose a constructor Bicycle(int wheels) ?
子类继承了超类的所有成员,但构造函数不被视为成员,这就是为什么构造函数不会被子类继承,但我们可以使用
super
关键字从子类调用超类。Subclasses inherit all the members from their superclass, but a constructor is not consider to be a member, that's why constructors are not inherited by subclasses, but we can call superclass from subclass using
super
keyword.