我想了解如何inhertitance和ArrayList的工作,所以我有这个下面的代码 它有一类就是数据将被保存继承型铸造编译错误
public class Database {
ArrayList<Student> students;
ArrayList<Course> courses;
public boolean doesIDExist(ArrayList<RegistrationSystem> array, int id){
boolean exist = false;
for (RegistrationSystem array1 : array) {
if (array1.getId() == id) {
exist = true;
break;
}
}
return exist;
}
public boolean addStudent(int id, String name, String surname){
if(doesIDExist(students, id)){
return false;
}
students.add(new Student(id, name, surname));
return true;
}
}
学生和课程数据库是登记制度
public class RegistrationSystem {
protected int id;
public int getId() {
return id;
}
}
的子类,但我得到这一行的错误:
if(doesIDExist(students, id))
incompatible types: ArrayList<Student> cannot be converted to ArrayList<RegistrationSystem>
我不明白为什么我会得到这个错误!
你可能想使该'RegistrationSystem'超类,而不是一个接口......学生说“是”或RegistrationSystem课程“是一个” RegistrationSystem是没有意义的,所以有它们之间的继承关系可能也没有意义。 –