public static ArrayList<Lecturer> lecturers = new ArrayList<Lecturer>();
lecturers.add(Lecturer);
public Lecturer (String name, String id, String address, String email, String office, String phone_number, String research, Module mod){
super(name, id, address, email, office, phone_number);
this.research = research;
this.mod = mod;
}
大家好, 我创建了一个类和一个名为Lecturer的构造函数,用于存储有关讲师的详细信息。我还创建了一个Lecturer类型的ArrayList,它存储讲师的对象。但是,当我尝试添加到列表时,我不断收到“预期”错误。我知道“添加(讲师)”部分有问题,但我似乎无法弄清楚还有什么要写。任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
尝试添加实例(并在方法内):
public void addLecturer(...) {
//
lecturers.add(new Lecturer(....));
}
更完整的版本
class Lecturers {
public static ArrayList<Lecturer> lecturers = new ArrayList<Lecturer>();
public static void addLecturer(Lecturer lecturer) {
lecturers.add(lecturer);
}
public static void main(String[] args) {
Lecturer one = new Lecturer(...)
addLecturer(one);
}
}
// you should extend a super Lecturer type
class Lecturer extends ... {
public Lecturer(String name, String id, String address, String email, String office, String phone_number, String research, Module mod) {
super(name, id, address, email, office, phone_number);
this.research = research;
this.mod = mod;
}
}
答案 1 :(得分:0)
尝试
lecturers.add(new Lecturer(name, id.....etc));
创建对象时需要使用new
运算符,而不是Class本身!