Java泛型与通配符 [英] Java Generics with wildcard
问题描述
有什么办法可以解决这种情况(我试图尽可能简单地处理场景) public class Test {
public static void main(String [] args){
$ b $ * / $
*这里我想指出CollectionGeneric可以是
*扩展Animal的东西(但构造函数不允许
*通配符)
* /
CollectionGeneric< ;?延伸动物> animalsCollectionGeneric = new CollectionGeneric< Animal>();
列表< ;?延伸动物>动物= getAnimals();
/ *为什么我不能这样做? * /
animalsCollectionGeneric.setBeans(animals);
}
private static List <?延伸动物> getAnimals(){
返回新的ArrayList< Dog>();
}
}
class CollectionGeneric< T> {
私人列表< T>豆;
public List< T> getBeans(){
return(beans!= null)? beans:new ArrayList< T>();
}
public void setBeans(List< T> bean){
this.beans = beans;
interface Animal {}
class Dog implements Animal {}
这种情况给了我下一个错误:
在类型
CollectionGeneric< capture#2-of?中设置setBeans(List< capture#2-of?extends Animal>)延伸动物>不适用于
的参数(List< capture#3-of?extends Animal>)*
我不确定是否有一种方法可以用泛型来做到这一点, 解决方案
这意味着两个集合不能被证明具有相同的类型边界:
CollectionGeneric< ;?延伸动物> animalsCollectionGeneric =
新CollectionGeneric< Animal>();
列表< ;?延伸动物>动物= getAnimals()
第一个可能在运行时具有 CollectionGeneric< Tiger> ;
和第二个列表< Gnu>
。混合这些将意味着你失去了类型安全性(更不用说大屠杀)。
因此你需要向编译器证明这两者是相关的,所以你的通用签名应为:
public void setBeans(List< ;? extends T> beans){}
public List< T> getBeans();
并用作:
列表与LT ;?延伸动物> beans = getBeans();
GenericCollection< Animal>动物=新的GenericCollection< Animal>();
animals.add(beans);
There is any way to fix this situation (I have try to simplyfy the scenario as much as i could):
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{}
this scenario is giving me the next error:
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>)*
I am not sure about if there is a way to do this with generics,
What this means is that the two collections can not be proved to have the same type bounds:
CollectionGeneric<? extends Animal> animalsCollectionGeneric =
new CollectionGeneric<Animal>();
List<? extends Animal> animals = getAnimals()
The first one might at runtime have CollectionGeneric<Tiger>
and the second one List<Gnu>
. Mixing those would mean you lose the type safety ( not to mention the carnage ).
Therefore you need to prove to the compiler that those two are related, so your generic signatures should be:
public void setBeans(List<? extends T> beans) {}
public List<T> getBeans();
and used as:
List<? extends Animal> beans = getBeans();
GenericCollection<Animal> animals = new GenericCollection<Animal>();
animals.add(beans);
这篇关于Java泛型与通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!