隐式超级构造函数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.
这是我的人类:
public class Person {
public Person(String name, double DOB){
}
}
public class Student extends Person {
public Student(String Instructor) {
}
}
推荐答案
如果构造函数没有显式调用超类构造函数
,Java编译器会自动向no- argument
超类的构造函数。
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
有一个这样的构造函数,所以如果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/tutorial/ java / IandI / super.html :
(参见'SubClass Constructors'一节)
Reference: http://docs.oracle.com/javase/tutorial/java/IandI/super.html : (See under section 'SubClass Constructors')
所以每当处理参数化构造函数时, c $ c> 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屋!