java - 隐式 super 构造函数 Person() 未定义。必须显式调用另一个构造函数?

标签 java class constructor superclass

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

这是我的person类:

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

    }
}

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

public class Student extends Person {

    public Student(String Instructor) {

    }

}

最佳答案

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.

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

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

关于java - 隐式 super 构造函数 Person() 未定义。必须显式调用另一个构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23395513/

相关文章:

java - 如何使用名称为 "dot"的 json simple 创建 json 对象?

java - 当我尝试使用 BiCionsumer 从列表中删除值时,它不起作用

java - 为什么Android中的调用栈是8KB?

c# - 使用按钮在两个类之间共享方法

java - 是否可以通过调用类方法来调用参数化构造函数?

java - 文本相似度算法

Python 如何在类定义中初始化 Reader 对象

php - 使用 PHP 和 CSS 使 <li> 活跃起来

c++ - 从 C++17 中的另一个构造函数调用具有不同参数类型的构造函数

c++ - 将 boost::bind 与构造函数一起使用