转换Collection< MyType>收集<对象> [英] convert Collection<MyType> to Collection<Object>
本文介绍了转换Collection< MyType>收集<对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public UniqueField(Collection< Object> items){
this.items =物品;
}
Collection< Object>
是我可以使用集合< OtherType>
。
在做:
集合< OtherType> collection = ...
UniqueField(collection);
我得到无效参数的编译错误。如何解决这个问题?
解决方案
您必须使用它而不是
public UniqueField(Collection< ;? extends Object> items){
this.items = items;
}
或?因为它等于?extends Object
public UniqueField(Collection<> items){
this .items =物品;
}
你可以看到这里为原因
I have a class with the following constructor:
public UniqueField(Collection<Object> items) {
this.items=items;
}
The idea behind the Collection<Object>
is that I would be able to use Collection<OtherType>
.
When doing:
Collection<OtherType> collection=...
new UniqueField(collection);
I getting a compile error of invalid argument. How can I fix this?
解决方案
You have to use this instead
public UniqueField(Collection<? extends Object> items) {
this.items=items;
}
or ? because it is equal to "? extends Object"
public UniqueField(Collection<?> items) {
this.items=items;
}
You can see here for the reason
这篇关于转换Collection< MyType>收集<对象>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文