子类是否从其超类继承构造函数?

发布于 2024-10-20 13:48:25 字数 123 浏览 3 评论 0原文

在子类中,我们可以使用子类的构造函数来初始化数据成员,该构造函数在内部调用超类的构造函数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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

陌伤ぢ 2024-10-27 13:48:25

子类的构造函数可以调用超类的构造函数,但它们不会被继承。

需要明确的是,这意味着如果您有类似以下内容:

public class Super
{
    public Super(int x)
    {
    }
}

public class Sub extends Super
{
    public Sub()
    {
        super(5);
    }
}

那么您不能编写:

new Sub(10);

因为没有 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:

public class Super
{
    public Super(int x)
    {
    }
}

public class Sub extends Super
{
    public Sub()
    {
        super(5);
    }
}

then you can't write:

new Sub(10);

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:

Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.

长不大的小祸害 2024-10-27 13:48:25

子类不能继承其超类的构造函数。

构造函数是类的特殊函数成员,因为它们不被子类继承。构造函数用于在创建时为对象提供有效状态。

主要原因之一是因为您可能不想覆盖超类构造函数,如果它们是继承的,则这是可能的。通过让开发人员能够重写超类构造函数,您将削弱该语言的封装能力。

另请参阅:构造函数永远不会被继承

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

秋心╮凉 2024-10-27 13:48:25

super 是 Java 关键字引用超类,super() 是调用超类构造函数的方法。构造函数不是继承的,但您仍然可以调用它。

super is a Java keyword for referring to the superclass, and super() is the way to call your superclass's constructor. The constructor is not inherited but you are still able to call it.

洋洋洒洒 2024-10-27 13:48:25

不..根本不会...子类不会继承其超类构造函数..相反,它可以使用关键字“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()"...

千里故人稀 2024-10-27 13:48:25

首先,构造函数不是类的成员,只有成员是被继承的。

其次,我们可以想象一些情况,我们不希望子类具有与父类相同的构造函数。

想象一个带有构造函数 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) ?

猫七 2024-10-27 13:48:25
public class Super
{
    public Super(int x)
    {
    }
}

public class Sub extends Super
{
    public Sub(int x)
    {
        super(x); // call constructor from Super class 
    }
}
Sub obj = new Sub(5);
public class Super
{
    public Super(int x)
    {
    }
}

public class Sub extends Super
{
    public Sub(int x)
    {
        super(x); // call constructor from Super class 
    }
}
Sub obj = new Sub(5);
╭ゆ眷念 2024-10-27 13:48:25

子类继承了超类的所有成员,但构造函数不被视为成员,这就是为什么构造函数不会被子类继承,但我们可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文