Java 错误:隐式超级构造函数未定义。必须显式调用另一个构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25209586/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:34:52  来源:igfitidea点击:

Java error: Implicit super constructor is undefined. Must explicitly invoke another constructor

java

提问by skk

I have a BaseClass in a external jar, it has a constructor setting Implementation class(JerseyClientImpl) to jerseyClient.

我在外部 jar 中有一个 BaseClass,它有一个构造函数设置 implementation class(JerseyClientImpl) 到 jerseyClient。

public BaseClass(AuthDetails auth, String ID) {
    setListID(D);
    this.jerseyClient = new JerseyClientImpl(auth);
}

I am extending the BaseClass to set my own Implementation class to jerseyClient , but i am getting the error mentioned. Changing the BaseClass to add default constructor is not in my control as i said its an external jar.Can you suggest how can i overcome this error.

我正在扩展 BaseClass 以将我自己的 Implementation 类设置为 jerseyClient ,但我收到了提到的错误。更改 BaseClass 以添加默认构造函数不在我的控制范围内,因为我说它是一个外部 jar。你能建议我如何克服这个错误。

采纳答案by Eran

Since BaseClasshas a non default constructor, it doesn't have the automatically generated parameterless default contstructor.

由于BaseClass具有非默认构造函数,因此它没有自动生成的无参数默认构造函数。

Therefore your sub-class can't rely on the default constructor (since it won't be able to call the non-existing default constructor of the base class), so your sub-class must have an explicit constructor that calls the constructor of the base class.

因此你的子类不能依赖默认构造函数(因为它不能调用基类不存在的默认构造函数),所以你的子类必须有一个显式构造函数来调用基类。

Either a constructor with the same parameters :

具有相同参数的构造函数:

public SubClass(AuthDetails auth, String ID) {
    super(auth,ID);
    ...
}

Or a constructor without parameters that gives default values for the base-class's constructor :

或者一个没有参数的构造函数,它为基类的构造函数提供默认值:

public SubClass() {
    super(null,"something");
    ...
}

回答by rgettman

In Java, if you don't explicitly provide a call to a superclass constructor as the first statement in a constructor, then it will insert an implicit call to the default superclass constructor. If there is no default superclass constructor, then you get the error you have mentioned.

在 Java 中,如果您没有显式提供对超类构造函数的调用作为构造函数中的第一条语句,那么它将插入对默认超类构造函数的隐式调用。如果没有默认的超类构造函数,则会出现您提到的错误。

The JLS, Section 8.8.7, states:

JLS,第8.8.7规定:

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 implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

如果构造函数体不以显式构造函数调用开始,并且被声明的构造函数不是原始类 Object 的一部分,则构造函数体以超类构造函数调用“super();”隐式开始,即对它的直接超类不带参数。

You must explicitly call the superclass constructor, passing all arguments, with something like this:

您必须显式调用超类构造函数,传递所有参数,如下所示:

public JerseyClientImpl(AuthDetails auth, String ID) {
    super(auth, ID);

    // Rest of constructor code
}

回答by HoneyBee

First of all, if you are writing some parameterized constructor in a class...the default no arg constructor of the class does not exist anymore.

首先,如果您在类中编写一些参数化构造函数......该类的默认无参数构造函数不再存在。

And, when you try to create constructor of its child class, the no-arg constructor of parent class is always called first.If it doesn't exist, you get compiler error.

而且,当您尝试为其子类创建构造函数时,总是首先调用父类的无参数构造函数。如果它不存在,则会出现编译器错误。

So, define the no arg constructor in your parent class, OR just call the parameterized constructor of parent class with some value inside the child class constructor.

因此,在您的父类中定义无参数构造函数,或者只在子类构造函数中使用一些值调用父类的参数化构造函数。