我正在做一个项目,我收到错误“隐式 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/