简体   繁体   English

Java:无法使用通用列表 <? extends Parent> 我的列表

[英]Java: Can't to generic List<? extends Parent> mylist

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 . 具体来说,您正在尝试将new Child()添加到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> . 可以使用多种列表来调用myfunction() ,而仅当列表为List<Parent>List<Child>才能添加new 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. 在最后一行中,使用OtherChild对象列表调用myfunction() Obviously, adding a Child object into such a list is illegal. 显然,将Child对象添加到此类列表中是非法的。 The compiler prevents that by rejecting the definition of myfunction() 编译器通过拒绝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 ). 如果希望myfunction()能够向myList添加元素, myList需要使用工厂(因为Java不允许使用new T() ,其中T是类型参数-由于类型被擦除 )。 Here's how myfunction() should look like: 这是myfunction()样子:

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(); }
  });
}

What List<? extends Parent> 什么List<? extends Parent> List<? extends Parent> means is "a list that contains only instances of some unknown subclass of Parent " - and since you don't know what subclass it is, you can't add anything except null to the list (you can get Parent instances from it, though). List<? extends Parent>的意思是“包含的一些未知的子类的实例只列出Parent ” -因为你不知道什么继承它是,但你不能添加任何null的列表(你可以得到Parent的情况下,但是)。

If you have a specific subclass Child that you want to add instances of to the list, then what you really need is this: 如果您有一个特定的子类Child想要将其实例添加到列表中,那么您真正需要的是:

public List<Child> myfunction(List<Child> mylist)

Following on Jon Skeet's example in the other question , let's say you used 另一个问题中的乔恩·斯基特(Jon Skeet)的示例之后,假设您使用了

public List<? extends Fruit> myfunction(List<? extends Fruit> mylist){
       mylist.add(new Apple())  ; //Doesn't Compile
       return myList;
}

but then called it with 但后来用

List<Banana> bananas = new ArrayList<Banana>();
bananas = myfunction(bananas);

You can't add an apple to a list of bananas. 您不能将苹果添加到香蕉列表中。

What you can do is 可以做的是

public List<Fruit> addApple(List<Fruit> mylist){
       mylist.add(new Apple())  ; 
       return myList;
}

public List<Fruit> addBanana(List<Fruit> mylist){
       mylist.add(new Banana())  ; 
       return myList;
}

如何添加return语句,或声明方法以返回void ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM