【问题标题】:implicit super constructor Person() is undefined. Must explicitly invoke another constructor?隐式超级构造函数 Person() 未定义。必须显式调用另一个构造函数?
【发布时间】:2014-06-17 05:05:24
【问题描述】:

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

这是我的person类:

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

    }
}

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

public class Student extends Person {

    public Student(String Instructor) {

    }

}

【问题讨论】:

  • public Student(String instructor)的第一行尝试super("Name", dob);
  • 不调用合适的超级构造函数,学生怎么知道它的名字和DOB是什么

标签: java class constructor superclass


【解决方案1】:

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

如果超类没有 没有参数的构造函数,你会得到一个编译时错误。目的 确实有这样的构造函数,所以如果 Object 是唯一的超类, 没有问题。

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

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

【讨论】:

    【解决方案2】:

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

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

    您不能只调用super(),因为该构造函数没有定义。

    【讨论】:

      【解决方案3】:

      这就是我实现它的方式(在我的例子中,超类是 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);
          }
      }
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          如果不调用其超类的构造函数,则无法创建实例。而且 jvm 不知道如何从 Student(String) 构造函数调用 Person(String, double)。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-09-29
            • 2014-11-06
            • 2023-04-08
            • 2013-03-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-02
            相关资源
            最近更新 更多