简体   繁体   English

在Java中将子类添加到通配符类型的泛型集合

[英]Add subclass to wildcard-typed generic collection in java

This is legal: 这是合法的:

List<? extends Animal> animals = getMonkeys();
...
List<Monkey> getMonkeys(){
    ...
}

The following results in a compiler error: 以下导致编译器错误:

animals.add(new Donkey());

Why? 为什么? How can I add a subtype legally? 如何合法添加子类型?

The compiler doesn't know which subclass the wildcard could be. 编译器不知道通配符可能是哪个子类。 It could be a List<Fish> for all it knows. 据了解,它可能是List<Fish> To preserve type safety, the compiler must prevent a call to add , because one shouldn't be allowed to add a Monkey to what could be a List<Fish> . 为了保持类型安全,编译器必须阻止调用add ,因为不应允许将Monkey添加到可能为List<Fish>

List<? extends Animal> animals = new ArrayList<Fish>();  // legal
animals.add(new Monkey());  // unsafe; compiler error
animals.add(new Donkey());  // also unsafe; compiler error

To prevent this, you need to eliminate the wildcard in your variable, so that the compiler knows the generic type parameter. 为防止这种情况,您需要消除变量中的通配符,以便编译器知道通用类型参数。 A List<Animal> or a List<Monkey> (or a List<Donkey> as the case may be) would allow you to call add with an argument of something other than null . List<Animal>List<Monkey> (或List<Donkey>视情况而定))将允许您使用非null的参数调用add If you must call getMonkeys() , then you must use its return type of List<Monkey> . 如果必须调用getMonkeys() ,则必须使用其返回类型List<Monkey>

List<Monkey> monkeys = getMonkeys();
monkeys.add(new Monkey());

Just declare the list as List<Animal> . 只需将列表声明为List<Animal> Any object which class extends from Animal will be able to be inserted there. 任何来自Animal类扩展的对象都可以插入到那里。

Use wildcard in a List<? extends Foo> List<? extends Foo>使用通配符List<? extends Foo> List<? extends Foo> when you're traversing the elements of the collection and want/need that the elements in the collection belong to a specific class. 当您遍历集合的元素并且希望/需要集合中的元素属于特定类时, List<? extends Foo> For example: 例如:

class Animal {
    public String getSpecie() {
        return "generic animal";
    }
}

class Donkey extends Animal {
    @Override
    public String getSpecie() {
        return "generic donkey";
    }
}

class Mokney extends Animal {
    @Override
    public String getSpecie() {
        return "generic monkey";
    }
}

//some method in an utility class...
//we are declaring that only List<Animal> or List<some class that extends animal> can be passed as argument
public void printSpecies(List<? extends Animal> animalList) {
    //we can be sure every element in animalList is an animal always
    for (Animal animal : animalList) {
        System.out.println(animal.getSpecie());
    }
}

//calling the method above...
List<Monkey> monkeyList = ...
printSpecies(monkeyList); //compiles and works
List<Donkey> donkeyList = ...
printSpecies(donkeyList); //compiles and works
List<String> stringList = ...
printSpecies(stringList); //doesn't compile

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

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