我正在编写一组与抽象超类相关联的子类。有一个默认构造函数,但是当我创建另一个构造函数时,它会给我以下错误:
Implicit super constructor Event() is undefined. Must explicitly invoke another constructor
我的代码如下:
public class Meeting extends Event {
private String location;
private String subject;
private String notes;
private String attendeeName;
// Array of attendees as string
private String[] listofAttendees = new String[10];
public Meeting(Date dueDate, Date reminderDate, String location,
String subject, String notes) {
super(dueDate, reminderDate);
this.location = location;
this.subject = subject;
this.notes = notes;
}
public Meeting(String attendeeName) { // this is the error constructor
this.attendeeName = attendeeName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String[] addAttendee(String name) {
// adding for loop for adding the list of attendees to the array
for (int i = 0; i < listofAttendees.length; i++) {
// array index(i) = name of attendee
listofAttendees[i] = name;
}
return listofAttendees;
}
}
答案 0 :(得分:0)
您正在扩展的对象Event
没有不带参数的构造函数。所以你需要调用super
并像在其他构造函数中那样指定参数。
答案 1 :(得分:0)
在你的构造函数中:
public Meeting(String attendeeName) {
this.attendeeName = attendeeName;
}
你并没有像在你的其他构造函数中那样明确地调用super。因此,Java隐式地尝试在基类上调用默认构造函数,即Event()。您需要在Event中定义默认构造函数,或者需要在此构造函数中将一个显式调用添加到其中一个已定义的Event构造函数中。