一个子类继承从它的超类的构造函数? [英] Does a subclass inherit constructors from it super class?

查看:360
本文介绍了一个子类继承从它的超类的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

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

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);
    }
}

那么你不能写:

new Sub(10);

,因为没有 Sub(int)

将构造函数看作是未初始化的对象的隐式参数的非继承静态方法可能会有帮助。

It may be helpful to think of constructors as uninherited static methods with an implicit parameter of the object being initialized.

Java语言规范第8.8节


构造函数声明不是成员。它们从不继承,因此不会隐藏或覆盖。

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

这篇关于一个子类继承从它的超类的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆