未定义的构造函数 (java) [英] Undefined constructor (java)
问题描述
所以我有一个名为 User 的 java 类,它包含一个像这样的构造函数:
so I have a java class called User that contains a constructor like this:
public class User extends Visitor {
public User(String UName, String Ufname){
setName(UName);
setFname(Ufname);
}
}
然后他的问题出现在我的另一个名为 Admin 的班级中:
And then the he problems occur in my other class called Admin:
public class Admin extends User { //error "Implicit super constructor User() is undefined for default constructor. Must define an explicit constructor"
//public Admin() { } //tried to add empty constructor but error stays there
//}
public void manage_items() {
throw new UnsupportedOperationException();
}
public void manage_users() {
throw new UnsupportedOperationException();
}
}
我收到错误 默认构造函数未定义隐式超级构造函数 User().必须在代码中标记的地方定义一个显式构造函数.
我不知道如何解决这个问题,而且我真的是 Java 新手.
I am getting the error Implicit super constructor User() is undefined for default constructor. Must define an explicit constructor at the place marked in the code.
I don't know how to fix that, and I'm really new to java.
推荐答案
你必须在第一个扩展 Visitor
的构造函数上调用 Visitor
的构造函数的类,使用 super()
:
You must call the Visitor
's constructor on the first like of the constructor that extends the Visitor
's class, using super()
:
public User(String UName,String Ufname){
super();
setName(UName);
setFname(Ufname);
}
如果您也希望 Admin 拥有自定义构造函数,则必须调用:
and if you want Admin to have a custom constructor too, you must call:
super(uname, ufname) // String, String - pass the params of the superclass cunstructor
所以:
public Admin() {
super(someString, someString);
}
这篇关于未定义的构造函数 (java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!