简体   繁体   English

Java下界类型List作为参数

[英]Java lower bounded type List as parameter

Hello I got two classes: Person class and Employee class which extends Person they look really simple 您好,我得到了两个类:Person类和Employee类,它们扩展了Person,它们看起来非常简单

public class Person {
    private String name;
    private String surname;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public Person(String name, String surname) {
        super();
        this.name = name;
        this.surname = surname;
    }


}

public class Employee extends Person {
    private String salary;
    private String position;
    public String getSalary() {
        return salary;
    }
    public void setSalary(String salary) {
        this.salary = salary;
    }
    public String getPosition() {
        return position;
    }
    public void setPosition(String position) {
        this.position = position;
    }
    public Employee(String name, String surname, String salary, String position) {
        super(name, surname);
        this.salary = salary;
        this.position = position;
    }



}

I want to write a function which will be adding Person to lower bounded parametrized List 我想编写一个将Person添加到下界参数化列表的函数

public class AuxiliaryClass {
    public static void addToList(List<? super Employee> list){
        Person osoba1 = new Person("Czeslaw","Spiewa");
        list.add(osoba1);
    }


}

But eclipse doesn't allow for it with error message: "The method add(capture#1-of ?super Employee)in the type List is not applicable for the arguments (Person) 但是eclipse不允许出现错误消息:“列表类型中的方法add(capture#1-of?super Employee)不适用于参数(人)

List<? super Employee> List<? super Employee> means "a list of some unknown but concrete class X, that is a superclass of Employee . List<? super Employee>意思是“一些未知但具体的类X的列表,它是Employee超类

It should be clear now why you cannot add an object of type Person to this list: Person is not X . 现在应该清楚为什么不能将Person类型的对象添加到此列表中: Person不是X

Perhaps, we could be of more help, if you explained what it is you are really trying to do here. 也许,如果您解释了您的意思,那么我们可能会提供更多帮助。

Update : Actually, I take it back. 更新 :实际上,我收回了它。 Person is X , because it is a subclass of Employee , which is a subclass of X . Person X ,因为它是Employee的子类, EmployeeX的子类。 It should be possible to add it to the list. 应该可以将其添加到列表中。 Looks like eclipse is just full of it. 看起来月食就充满了。 Javac (1.8) lets me do this, and so does intellij. Javac(1.8)允许我执行此操作,intellij也是如此。

Sorry for the confusion. 对困惑感到抱歉。

You don't allowed to put Person instances into the List because when you call your function you can provide for example List collection. 您不允许将Person实例放入列表,因为调用函数时可以提供例如List集合。 If you will be able to put Person into this collection, this would cause ClassCastException on accessing this collection content, because every collection item will be treated as Employee (but Person instance cannot be casted to Employee). 如果您能够将Person放入此集合中,这将导致在访问此集合内容时发生ClassCastException,因为每个集合项都将被视为Employee(但Person实例不能转换为Employee)。

You should take a look at: https://en.wikipedia.org/wiki/Liskov_substitution_principle . 您应该看一下: https : //en.wikipedia.org/wiki/Liskov_substitution_principle

In this case you want to put Persion in list of Employee but not every persion is a Employee and that is why it fails. 在这种情况下,您想将Persion放在Employee列表中,但并不是每个Persion都是Employee,这就是为什么它失败的原因。 You would be able to to that if the list was expecting Persons or anything that extends Person. 如果列表中包含“期望人物”或任何扩展“人物”的内容,您将能够做到这一点。

List<? extends Person>

Then you could add a person but also a employee since every employee is a person. 然后,您可以添加一个人,但也可以添加一个员工,因为每个员工都是一个人。

To be clear I don't want to add to the List an object which is subtype of type Person. 为了清楚起见,我不想在列表中添加一个类型为Person的子类型的对象。 I want to add an object which is supertype (ancestor) of Employee type. 我想添加一个对象,它是Employee类型的超类型(祖先)。 I want to know if operation described above is possible? 我想知道上述操作是否可行?

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

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