隐式超级构造函数 Person() 未定义.必须显式调用另一个构造函数? [英] implicit super constructor Person() is undefined. Must explicitly invoke another constructor?
问题描述
我正在处理一个项目,但收到错误隐式超级构造函数 Person() 未定义.必须显式调用另一个构造函数",我不太明白.
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){
}
}
我的学生类在尝试实现 person 类时,并给它一个教师变量.
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) {
}
}
推荐答案
如果构造函数没有显式调用超类构造函数,Java 编译器自动插入对无参数的调用超类的构造函数.
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.
如果超类没有没有参数的构造函数,你会得到一个编译时错误.目的确实有这样的构造函数,所以如果 Object 是唯一的超类,没有问题.
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/教程/java/IandI/super.html:(参见子类构造函数"部分)
Reference: http://docs.oracle.com/javase/tutorial/java/IandI/super.html : (See under section 'SubClass Constructors')
因此,每当处理参数化构造函数时,都会对父构造函数进行 super(parameter1, parameter2 ..)
调用.此外,这个 super() 调用应该是构造函数块中的第一行.
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.
这篇关于隐式超级构造函数 Person() 未定义.必须显式调用另一个构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!