我正在尝试实现具有比父类更多参数的类的构造函数,唯一的共同点是标题。当我尝试在Book类中实现构造函数时,它向我显示错误“隐式超级构造函数Item()未定义”。
public class Book extends Item {
private String author = "";
private String ISBN = "";
private String publisher = "";
public Book(String theTitle, String theAuthor, String theIsbn, String thePublisher){
}
}
父类构造函数;
public abstract class Item {
private String title;
private int playingTime;
protected boolean gotIt;
private String comment;
public Item(String title, int playingTime, boolean gotIt, String comment) {
super();
this.title = title;
this.playingTime = playingTime;
this.gotIt = gotIt;
this.comment = comment;
}
提前致谢。
答案 0 :(得分:5)
你的超类没有no-args默认构造函数,所以你必须使用super()关键字传递默认值来显式调用超类的重载构造函数。
public Book(String theTitle, String theAuthor, String theIsbn, String thePublisher){
super(thTitle,0,false,null)
}
答案 1 :(得分:0)
因为你没有定义无参数构造函数,或者在super(....)
中提供参数答案 2 :(得分:0)
public Item(String title, int playingTime, boolean gotIt, String comment) {
super(); //remove this constructor or define no-arg constructor in super class.
super(thTitle,0,false,null); //add this constructor
}