Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 继承类型转换编译错误_Java_Inheritance_Arraylist - Fatal编程技术网

Java 继承类型转换编译错误

Java 继承类型转换编译错误,java,inheritance,arraylist,Java,Inheritance,Arraylist,我试图理解Inheritance和ArrayList是如何工作的,所以我有以下代码 它有一个类,它是保存数据的数据库 public class Database { ArrayList<Student> students; ArrayList<Course> courses; public boolean doesIDExist(ArrayList<RegistrationSystem> array, int id){

我试图理解Inheritance和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;
    }

}
但我在这行中得到一个错误:

       if( doesIDExist(students, id))
不兼容类型:ArrayList无法转换为ArrayList


我不太明白为什么我会犯这个错误

您的方法需要注册系统列表,而不是学生列表

您必须更改为:

public boolean doesIDExist(ArrayList<? extends RegistrationSystem> array, int id){

你在这里要做的是这样的:

ArrayList数组=学生

Java不允许这样做


更改方法签名改为doesIDExistArrayList当方法仅接受RegistrationSystem时,您正在尝试传递学生对象的ArrayList。

在这里,如果Doesidexistudents,id,则传递的是学生对象,而不是RegistrationSystem对象。作为更好的选择,您必须实现一种新方法来识别现有学生id的

,尽管学生是注册系统的一个子类型,但ArrayList不是ArrayList的一个子类型

这样做的原因是,这可能会导致非类型安全代码。考虑下面的代码:

void method1(ArrayList<RegistrationSystem> listOfRegistrationSystems) {
    listOfRegistrationSystems.add(new Course()); 
}
void method2() {
    ArrayList<Student> listOfStudents = new ArrayList<>();
    method1(listOfStudents);
    Student student = listOfStudents.get(0);
    // Ooops! The first element of listOfStudents is not a Student!
}

也许有必要更详细地解释一下上界通配符,而不仅仅是在这里介绍解决方案。请参阅以扩展@Jens answer,ArrayList或ArrayList都不是ArrayList的子类型;您必须使用通配符语法吗?在此场景中扩展RegistrationSystem,使您的方法足够灵活,可以接受RegistrationSystem的任何子类型的集合您可能希望将该RegistrationSystem超类改为接口。。。说学生是注册系统或者说课程是注册系统是没有意义的,所以他们之间有继承关系可能也没有意义。很好的解释。除此之外,值得注意的是:泛型类型本身可以在其子类型之一的调用语句中被替换,例如,采用列表的方法
void method1(ArrayList<RegistrationSystem> listOfRegistrationSystems) {
    listOfRegistrationSystems.add(new Course()); 
}
void method2() {
    ArrayList<Student> listOfStudents = new ArrayList<>();
    method1(listOfStudents);
    Student student = listOfStudents.get(0);
    // Ooops! The first element of listOfStudents is not a Student!
}
public boolean doesIDExist(ArrayList<? extends RegistrationSystem> array, int id){
    boolean exist = false;
    for (RegistrationSystem array1 : array) {
        if (array1.getId() == id) {
            exist = true;
            break;
        }
    }
    return exist;
}