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

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

implicit super constructor Person() is undefined. Must explicitly invoke another constructor?

javaclassconstructorsuperclass

提问by sirnomnomz

I am working on a project and i am getting the error "implicit super constructor Person() is undefined. Must explicitly invoke another constructor" and i don't quite understand it.

我正在处理一个项目,但收到错误“隐式超级构造函数 Person() 未定义。必须显式调用另一个构造函数”,我不太明白。

Here is my person class:

这是我的个人课程:

public class Person {
    public Person(String name, double DOB){

    }
}

And my student class when trying to implement the person class, and giving it an instructor variable.

和我的学生类在尝试实现人类时,并给它一个讲师变量。

public class Student extends Person {

    public Student(String Instructor) {

    }

}

采纳答案by pxm

If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

如果构造函数没有显式调用超类构造函数,Java 编译器会自动插入对超类的无参数构造函数的调用。

如果超类没有无参数构造函数,您将收到编译时错误。Object 确实有这样的构造函数,所以如果 Object 是唯一的超类,就没有问题。

Reference: http://docs.oracle.com/javase/tutorial/java/IandI/super.html: (See under section 'SubClass Constructors')

参考:http://docs.oracle.com/javase/tutorial/java/IandI/super.html :(参见“子类构造函数”部分)

So whenever dealing with parameterized constructors make a super(parameter1, parameter2 ..)call to the parent constructor. Also this super() call should be the FIRST line in your constructor block.

因此,每当处理参数化构造函数时,都会 super(parameter1, parameter2 ..)调用父构造函数。 此外,这个 super() 调用应该是构造函数块中的第一行。

回答by rgettman

When creating a subclass constructor, if you don't explicitly call a superclass constructor with super, then Java will insert an implicit call to the no-arg "default" superclass constructor, i.e. super();.

创建子类构造函数时,如果您没有使用 显式调用超类构造函数super,那么Java 将插入对无参数“默认”超类构造函数的隐式调用,即super();.

However, your superclass Persondoesn't have a no-arg constructor. Either supply an explicit no-arg constructor in Personor explicitly call the existing superclass constructor in the Studentconstructor.

但是,您的超类Person没有无参数构造函数。在构造函数中提供显式无参数构造函数Person或显式调用现有超类构造Student函数。

回答by Rogue

You need to make a supercall to your defined constructor:

您需要super调用您定义的构造函数:

public Student(String instructor) {
    super(/* name */, /* date of birth */);
}

You can't just call super()because that constructor is not defined.

您不能super()因为未定义该构造函数而只是调用。

回答by EasterBunnyBugSmasher

you can't create an instance without calling a constructor of its super class. And the jvm doesn't know how to call Person(String, double) from your Student(String) constructor.

您不能在不调用其超类的构造函数的情况下创建实例。并且 jvm 不知道如何从您的 Student(String) 构造函数调用 Person(String, double) 。

回答by Jaime Montoya

This is how I achieved it (in my case the superclass was Team and the subclass was Scorer):

这就是我实现它的方式(在我的例子中,超类是 Team,子类是 Scorer):

// Team.java
public class Team {
    String team;
    int won;
    int drawn;
    int lost;
    int goalsFor;
    int goalsAgainst;
    Team(String team, int won, int drawn, int lost, int goalsFor, int goalsAgainst){
        this.team = team;
        this.won = won;
        this.drawn = drawn;
        this.lost = lost;
        this.goalsFor = goalsFor;
        this.goalsAgainst = goalsAgainst;
    }
    int matchesPlayed(){
        return won + drawn + lost;
    }
    int goalDifference(){
        return goalsFor - goalsAgainst;
    }
    int points(){
        return (won * 3) + (drawn * 1);
    }
}
// Scorer.java
public class Scorer extends Team{
    String player;
    int goalsScored;
    Scorer(String player, int goalsScored, String team, int won, int drawn, int lost, int goalsFor, int goalsAgainst){
        super(team, won, drawn, lost, goalsFor, goalsAgainst);
        this.player = player;
        this.goalsScored = goalsScored;
    }
    float contribution(){
        return (float)this.goalsScored / (float)this.goalsFor;
    }
    float goalsPerMatch(){
        return (float)this.goalsScored/(float)(this.won + this.drawn + this.lost);
    }
}