我想了解如何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是沒有意義的,所以有它們之間的繼承關係可能也沒有意義。 –