带通配符的 Java 泛型
有任何方法可以解决这种情况(我已尝试尽可能简化该场景):
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>)*
我不确定是否有办法用泛型来做到这一点,
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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着无法证明两个集合具有相同的类型界限:
第一个集合在运行时可能具有
CollectionGeneric
,第二个集合可能具有List
代码>. 混合这些将意味着你失去类型安全(更不用说大屠杀)。因此,您需要向编译器证明这两者是相关的,因此您的通用签名应该是:
并用作:
What this means is that the two collections can not be proved to have the same type bounds:
The first one might at runtime have
CollectionGeneric<Tiger>
and the second oneList<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:
and used as: