Java继承——构造函数

发布于 2024-11-13 10:33:27 字数 364 浏览 1 评论 0原文

在准备期末考试时,我在我目前正在学习的书中看到了以下陈述。考虑以下代码:

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

class B extends A {
    public B(int x ) {   }
}

是否必须在类 B(super(x)) 的构造函数中调用类 A 的构造函数。书中指出这不是强制性的,因为它们具有确切的参数数量和类型。但是当我在 java 编译器中尝试这个时,会抛出以下错误:

A 类中的构造函数 A 不能 适用于给定类型;必需的: 找到 int:没有参数原因: 实际和形式参数列表 长度不同

While studying for my finals, I came across the following statement in the book from which I am currently studying. Considering the following code :

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

class B extends A {
    public B(int x ) {   }
}

is it mandatory to call the constructor of class A in the constructor of class B(super(x)). The book states that it's not mandatory, because they have the exact number and type of parameters. But when I try this in a java compiler, the following error gets thrown :

constructor A in class A cannot be
applied to given types; required:
int found: no arguments reason:
actual and formal argument lists
differ in length

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

执着的年纪 2024-11-20 10:33:27

编译器会自动在开头插入 super()

然而,即使构造函数参数,super()(不带参数)也会被添加,它会调用超类的默认构造函数。而你没有,因此会出现错误。

您必须指定 super(x) (以调用 A(x)),或定义一个无参构造函数。

顺便说一下,Eclipse 编译器给出了更好的错误消息:

隐式超级构造函数 A() 未定义。必须显式调用另一个构造函数

The compiler automatically inserts super() in the beginning.

However, even constructors arguments, super() (without arguments) is added which invokes the default constructor of the superclass. And you don't have one, hence the error.

You have to specify super(x) (to invoke A(x)), or define a no-argument constructor.

By the way, Eclipse compiler gives a way better error message:

Implicit super constructor A() is undefined. Must explicitly invoke another constructor

百变从容 2024-11-20 10:33:27

看起来编译器尝试使用 super() 创建对超类默认构造函数的调用,但该调用不可用:

required: int
found:    no arguments

但是回到你的书:我从未听说过可以这样做的规则如果实际构造函数与直接超类中的构造函数具有完全相同的参数列表,则跳过构造函数中的 super 语句。仅隐式添加对超类默认构造函数的调用 (super()),但这要求超类具有一个默认构造函数。

与您书中所写的内容相反(或与您对书面文本的理解相反),这是语言规范中的一句话:

如果构造函数体不以显式构造函数调用开头
并且所声明的构造函数不是原始类 Object 的一部分,那么
编译器隐式假定构造函数主体以 super 开头
类构造函数调用“super();”,调用其构造函数
不带参数的直接超类。

It looks like the compiler tries to create a call to the superclasses default constructor with super(), which isn't avaliable:

required: int
found:    no arguments

But back to your book: I've never heard of a rule that you can skip the super statement in a constructor if the actual constructor has the exact same parameter list as a constructor in the direct superclass. Only a call to the superclass's default constructor is added implicitly (super()) but that requires that the superclass has a default constructor.

In contrast to what's written in your book (or in contrast to your understanding of the written text), here's a sentence from the language spec:

If a constructor body does not begin with an explicit constructor invocation
and the constructor being declared is not part of the primordial class Object, then
the constructor body is implicitly assumed by the compiler to begin with a super
class constructor invocation “super();”, an invocation of the constructor of its
direct superclass that takes no arguments.

你对谁都笑 2024-11-20 10:33:27

如果您的基类具有默认构造函数(无参构造函数),则当您扩展 B 时,您不需要显式调用 super(),因为它被称为任何方式。

但是当你有一个带参数的构造函数时,在B中制作带参数的构造函数时,需要传入super()一个A的参数>

示例:

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

  class B extends A {
    public B(int x ) 
    {
       super(x); // need to specify the parameter for class A
       //... 
    }
  }

If you have a the base class having a default constructor (no-arg constructor), when you extend B, you don't need to explicitly call super() because it is called any way.

But when you have a constructor with arguments, when making the contructor with parameters in B, you need to pass in super() a parameter for A

example :

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

  class B extends A {
    public B(int x ) 
    {
       super(x); // need to specify the parameter for class A
       //... 
    }
  }
三岁铭 2024-11-20 10:33:27

当您没有默认构造函数并且使用默认构造函数创建实例时,就会发生这种情况。因为如果你有任何参数化构造函数,那么编译器不会为你插入默认的构造函数,而是你必须定义。

This happens when you dont have a default constructor and you are creating an instance with default one. Because If you have any Parameterized constructor then compiler will not insert the default one for you, instead you have to define.

天生の放荡 2024-11-20 10:33:27

对于Java,需要调用超类的构造函数。因此,每当您生成子类的构造函数时,超类的构造函数都是由 IDE 自行创建的。

这是因为每当基类构造函数需要参数时,编译器都会认为它们将由基类构造函数填充。如果是默认构造函数,就可以了。无需在子类中调用super()。

It is neccessary to call constructor of super class in case of Java. Therefore, whenever you generate constructor of sub class, super class' constructor is self created by IDE.

It is because whenever base class constructor demands arguments, compiler thinks they are to be filled by base class constructor. In case of default constructor, it is OK. No need to call super() in sub class.

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