简体   繁体   English

创建一个方法来打印 java 中的子 arrayList 项目

[英]Create a method to print child arrayList items in java

I have an abstract Person class which has 3 children: Admin , Employee and Student .我有一个抽象的Person class ,它有 3 个孩子: AdminEmployeeStudent There is only one admin object.只有一名管理员 object。

I also have a class which it's "name" is Statics and holds all public data I need, like this:我还有一个 class ,它的“名称”是Statics并保存我需要的所有公共数据,如下所示:

public class Statics {  

private static Person currentLoginUser;
private static ArrayList<Person> people = new ArrayList<>();
private static ArrayList<Student> students = new ArrayList<>();
private static ArrayList<Employee> employees = new ArrayList<>();
//We don't need ArrayList<Admin> because there is only one admin in the whole program

//Adder and getter methods:
//...
}

You may ask what ArrayList<Person> in the code is?您可能会问代码中的ArrayList<Person>是什么? When I create an object, I add it to both "it's own type arraylist" and " ArrayList<Person> ".当我创建 object 时,我将它添加到“它自己的类型数组列表”和“ ArrayList<Person> ”中。 So ArrayList<Person> has everybody.所以ArrayList<Person>拥有所有人。

As I said, I only have 1 admin:正如我所说,我只有 1 个管理员:

Person admin = new Admin(); //`Admin` extends `Person`

I want to create a method (in Admin class) which takes an arrayList of Person as input and prints it's data.我想创建一个方法(在Admin类中),它将Person的 arrayList 作为输入并打印它的数据。 So I did this:所以我这样做了:

//Admin class:
public void printList(ArrayList<Person> people){
//Do something
}

Let's assume admin wants to see the list of the students: I call it like this:假设admin想要查看学生列表:我这样称呼它:

ArrayList<Student> s = Statics.getStudents();
((Admin)admin).printList(s); //admin object was created by `Person` class so I have to cast it to (Admin) to use `Admin`'s own methods.

It (eclipse) says that:它(日食)说:

The method printList(ArrayList< Person >) in the type Admin is not applicable for the arguments (ArrayList< Student >) Admin 类型中的 printList(ArrayList< Person >) 方法不适用于 arguments (ArrayList< Student >)

I tried to cast it to person:我试图将它投射到人:

((Admin)admin).printList((ArrayList<Person>)s);

this time I got this error:这次我收到了这个错误:

Cannot cast from ArrayList< Student > to ArrayList< Person >无法从 ArrayList< Student > 转换为 ArrayList< Person >

In this link the answer is to pass the main arrayList and check if it's object's are from type "Student" or not, then print it's value but I don't want to check the whole Person arrayList everytime, I just want a method, which takes and arrayList of Person 's children ( Employee , Student , etc) and prints them.在此链接中,答案是通过主要 arrayList 并检查它的对象是否来自“学生”类型,然后打印它的值,但我不想每次都检查整个Person arrayList,我只想要一个方法,获取Person的孩子( EmployeeStudent等)的 arrayList 并打印它们。

Your problem is with generics.你的问题是 generics。 Even though Student extends Person , List<Student> does not extend List<Person> .即使Student扩展了PersonList<Student>没有扩展List<Person> That's why java generics has the wildcard which is represented with a ?这就是为什么 java generics 具有用?表示的通配符的原因(question mark). (问号)。

If you use List<? extends Person>如果你使用List<? extends Person> List<? extends Person> that means that each element in the list is an instance of either Person or any class that extends Person . List<? extends Person>这意味着列表中的每个元素都是Person或扩展Person的任何 class 的实例。

Refer to Generics, Inheritance, and Subtypes in the Generics lesson of the Learning the Java Language trail in Oracle's java tutorials. Refer to Generics, Inheritance, and Subtypes in the Generics lesson of the Learning the Java Language trail in Oracle's java tutorials.

Change printList method signature to:printList方法签名更改为:

public void printList(ArrayList<? extends Person> people)

Explanation :说明

Changing the signature will allow you to pass an ArrayList parameterized with Person or its subclasses.更改签名将允许您传递使用Person或其子类参数化的ArrayList

In java the ? extends T在 java 中? extends T ? extends T construct is known as Upper Bounded Wildcards . ? extends T构造被称为上界通配符

When using the people inside the printList method you will be able to "consume" ( get ) people from the list (but not to "produce" ( add ) them).printList方法中使用people时,您将能够从列表中“消费”( get )人员(但不能“生产”( add )他们)。 "Consume" and "produce" are meant here in the PECS sense. “消费”和“生产”在这里是指PECS意义上的。


For more information about generics see Generics (Java tutorial)有关 generics 的更多信息,请参阅Generics(Java 教程)

As mentioned by @Abra problem is with generics.正如@Abra 所提到的,问题是generics。 You can use generics in your code to fix your problem.您可以在代码中使用 generics 来解决您的问题。

Create Admin class as a generic class and create printList method with List<T> list argument so that it will accept any type of object.创建Admin class 作为通用 class 并使用List<T> list参数创建printList方法,以便它接受任何类型的 object。 eg Person, Employee, Student etc.例如人、雇员、学生等。

This is just an example so please change your code accordingly.这只是一个示例,因此请相应地更改您的代码。

public class Admin<T> {

  public void printList(List<T> list) {
   // Print list here
  }
}
    Statics stat = new Statics();
    Admin admin = new Admin<Student>();

    // Students
    admin.printList(stat.getStudents());
    // Employees
    admin.printList(stat.getEmployees());
    // People
    admin.printList(stat.getPeople());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM