この状況を修正する方法はあります (可能な限りシナリオを単純化しようとしました):
public class Test {
public static void main(String[] args) {
/*
* HERE I would like to indicate that the CollectionGeneric can be of
* something that extends Animal (but the constructor doesn't allow
* wildcards)
*/
CollectionGeneric<? extends Animal> animalsCollectionGeneric = new CollectionGeneric<Animal>();
List<? extends Animal> animals = getAnimals();
/* Why I cannt do that? */
animalsCollectionGeneric.setBeans(animals);
}
private static List<? extends Animal> getAnimals() {
return new ArrayList<Dog>();
}
}
class CollectionGeneric<T> {
private List<T> beans;
public List<T> getBeans() {
return (beans != null) ? beans : new ArrayList<T>();
}
public void setBeans(List<T> beans) {
this.beans = beans;
}
}
interface Animal {}
class Dog implements Animal{}
このシナリオは私に次のエラーを与えています:
The method setBeans(List<capture#2-of ? extends Animal>) in the type
CollectionGeneric<capture#2-of ? extends Animal> is not applicable for
the arguments (List<capture#3-of ? extends Animal>)*
ジェネリックでこれを行う方法があるかどうかはわかりませんが、