Java:无法通用列表< ;? extends Parent> mylist [英] Java: Can't to generic List<? extends Parent> mylist
问题描述
另一个例子是相当复杂,我完全不明白,我的问题是类似的,但是我说它更简单,可能会产生更简单的答案
public List< ;? extends Parent> myfunction(List< ;? extends Parent> mylist){
mylist.add(new Child()); //不编译
}
我相信你正在寻找一个方法,它需要一个列表,添加一些东西,然后返回列表。该列表是通用的,您希望返回类型与参数的类型匹配。
通常情况下,您可以这样做:
public< T extends Parent>列表< T> myfunction(List< T> myList){
... //向myList添加一些东西
return myList;
}
具体来说,您正在尝试添加 ()
到 myList
。这不能工作。 myfunction()
可以使用多种列表来调用,而添加 new Child()
是 List< Parent>
或 List< Child>
。下面是一个不同类型列表的示例:
public static class Parent {}
public static class {}
public static class OtherChild extends Parent {}
public< T extends Parent>列表< T> myfunction(List< T> myList){
myList.add(new Child());
return myList;
}
myfunction(new ArrayList< OtherChild>());
在最后一行中, myfunction()
用 OtherChild
对象的列表调用。显然,将 Child
对象添加到此类列表中是非法的。编译器通过拒绝 myfunction()
附录 p>
如果你想让 myfunction()
能够添加一个元素到 myList
你需要使用一个工厂(因为Java不允许 new T()
其中T是一个类型参数 - 由于类型擦除)。下面是 myfunction()
的外观:
public interface Factory< T> {
public T create();
}
public< T extends Parent>列表< T> myfunction(List< T> myList,
Factory< ;? extends T> factory){
myList.add(factory.create());
return myList;
}
现在,它的用法:
public static ChildOfOtherChild extends OtherChild {}
myfunction(new ArrayList< OtherChild>(),new Factory< ChildOfOtherChild> $ b @Override public ChildOfOtherChild create(){return new ChildOfOtherChild();}
});
}
The other Example is pretty complex and I didn't understand it at all, my problem is similar somehow but as I said it's simpler and might yield in simpler answer
public List<? extends Parent> myfunction(List<? extends Parent> mylist){
mylist.add(new Child()) ; //Doesn't Compile
}
I believe you're looking for a method that takes a list, adds something to it, and then returns the list. The list is generic and you want the return type to match the type of the parameter.
Here's how you do it, in the general case:
public <T extends Parent> List<T> myfunction(List<T> myList) {
... // Add something to myList
return myList;
}
Specifically, you're trying to add new Child()
to myList
. This cannot work. myfunction()
may be called with many kinds of lists, whereas adding new Child()
can only work if the list is either a List<Parent>
or a List<Child>
. Here's an example for a List of different kind:
public static class Parent {}
public static class Child extends Parent {}
public static class OtherChild extends Parent {}
public <T extends Parent> List<T> myfunction(List<T> myList) {
myList.add(new Child());
return myList;
}
myfunction(new ArrayList<OtherChild>());
In the last line, myfunction()
is called with a List of OtherChild
objects. Obviously, adding a Child
object into such a list is illegal. The compiler prevents that by rejecting the definition of myfunction()
Appendix
If you want myfunction()
to be able to add an element to myList
you need to use a factory (since Java does not allow new T()
where T is a type parameter - due to type erasure). Here's how myfunction()
should look like:
public interface Factory<T> {
public T create();
}
public <T extends Parent> List<T> myfunction(List<T> myList,
Factory<? extends T> factory) {
myList.add(factory.create());
return myList;
}
And now, its usage:
public static class ChildOfOtherChild extends OtherChild {}
myfunction(new ArrayList<OtherChild>(), new Factory<ChildOfOtherChild>() {
@Override public ChildOfOtherChild create() { return new ChildOfOtherChild(); }
});
}
这篇关于Java:无法通用列表< ;? extends Parent> mylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!