List<之间的区别是什么?super T>和List<?延伸T> ?

我曾经使用List<?扩展T>,但它不允许我添加元素到它的List .add(e),而List<?super T>有。


当前回答

向列表中添加一个项目:

列表< ?extends X >不允许向列表中添加任何东西,除了null。 列表< ?super X >允许添加任何-a X (X或其子类型),或null。

从列表中获取一项:

当你从List< ?扩展X >,你可以将它赋值给X类型的变量或X的任何超类型,包括Object。 当你从List< ?super X >,你只能将它分配给Object类型的变量。

一些例子:

    List<? extends Number> list1 = new ArrayList<Integer>();
    list1.add(null);  //OK
    Number n = list1.get(0);  //OK
    Serializable s = list1.get(0);  //OK
    Object o = list1.get(0);  //OK

    list1.add(2.3);  //ERROR
    list1.add(5);  //ERROR
    list1.add(new Object());  //ERROR
    Integer i = list1.get(0);  //ERROR

    List<? super Number> list2 = new ArrayList<Number>();
    list2.add(null);  //OK
    list2.add(2.3);  //OK
    list2.add(5);  //OK
    Object o = list2.get(0);  //OK

    list2.add(new Object());  //ERROR
    Number n = list2.get(0);  //ERROR
    Serializable s = list2.get(0);  //ERROR
    Integer i = list2.get(0);  //ERROR

其他回答

何时使用extends和super

通配符在方法参数中最有用。它们允许方法接口中必要的灵活性。

人们经常混淆什么时候使用扩展,什么时候使用超边界。经验法则是“得到-放”原则。如果您从参数化容器中获取某些内容,请使用extends。

int totalFuel(List<? extends Vehicle> list) {
int total = 0;
for(Vehicle v : list) {
    total += v.getFuel();
}
return total;}

totalFuel方法从列表中获取车辆,询问它们有多少燃料,并计算总数。 如果将对象放入参数化容器中,请使用super。

int totalValue(Valuer<? super Vehicle> valuer) {
int total = 0;
for(Vehicle v : vehicles) {
    total += valuer.evaluate(v);
}
return total;}

totalValue方法将车辆放入Valuer中。 知道扩展界比super更常见是很有用的。

使用只能从集合中获得的扩展。你不能投入进去。此外,虽然超级允许获取和放置,在获取期间的返回类型是?超级T。

你可以通过上面所有的答案来理解为什么.add()被限制为'<?>”、“< ?扩展>',部分扩展到'<?超级>”。

但如果你想记住它,而不想每次都去探索答案,这里是所有问题的结论:

列表< ?extends A>表示它将接受A的任何List和A的子类。 但是你不能在这个列表中添加任何东西。甚至不是A类型的对象。

列表< ?super A>表示它将接受A的任何列表和A的超类。 您可以添加类型A的对象及其子类。

根据Bert F的回答,我想解释一下我的理解。

假设我们有3个类

public class Fruit{}

public class Melon extends Fruit{}

public class WaterMelon extends Melon{}

这里我们有

List<? extends Fruit> fruitExtendedList = …

//Says that I can be a list of any object as long as this object extends Fruit.

好的,现在让我们尝试从fruitExtendedList中获取一些值

Fruit fruit = fruitExtendedList.get(position)

//This is valid as it can only return Fruit or its subclass.

让我们再试一次

Melon melon = fruitExtendedList.get(position)

//This is not valid because fruitExtendedList can be a list of Fruit only, it may not be 
//list of Melon or WaterMelon and in java we cannot assign sub class object to 
//super class object reference without explicitly casting it.

的情况也是一样

WaterMelon waterMelon = fruitExtendedList.get(position)

现在让我们尝试在fruitExtendedList中设置一些对象

添加水果对象

fruitExtendedList.add(new Fruit())

//This in not valid because as we know fruitExtendedList can be a list of any 
//object as long as this object extends Fruit. So what if it was the list of  
//WaterMelon or Melon you cannot add Fruit to the list of WaterMelon or Melon.

添加甜瓜对象

fruitExtendedList.add(new Melon())

//This would be valid if fruitExtendedList was the list of Fruit but it may 
//not be, as it can also be the list of WaterMelon object. So, we see an invalid 
//condition already.

最后让我们尝试添加西瓜对象

fruitExtendedList.add(new WaterMelon())

//Ok, we got it now we can finally write to fruitExtendedList as WaterMelon 
//can be added to the list of Fruit or Melon as any superclass reference can point 
//to its subclass object.

但是等等,如果有人决定制造一种新的柠檬,让我们说,为了争论,SaltyLemon作为

public class SaltyLemon extends Lemon{}

现在fruitExtendedList可以是Fruit, Melon, WaterMelon或SaltyLemon的列表。

因此,我们的声明

fruitExtendedList.add(new WaterMelon())

也是无效的。

基本上,我们可以说不能向fruitExtendedList中写入任何内容。

这对List<?扩展了水果>

现在让我们看看

List<? super Melon> melonSuperList= …

//Says that I can be a list of anything as long as its object has super class of Melon.

现在让我们尝试从melonSuperList中获取一些值

Fruit fruit = melonSuperList.get(position)

//This is not valid as melonSuperList can be a list of Object as in java all 
//the object extends from Object class. So, Object can be super class of Melon and 
//melonSuperList can be a list of Object type

同样地,Melon、Melon或其他任何对象都不能读取。

但请注意,我们可以读取对象类型实例

Object myObject = melonSuperList.get(position)

//This is valid because Object cannot have any super class and above statement 
//can return only Fruit, Melon, WaterMelon or Object they all can be referenced by
//Object type reference.

现在,让我们尝试从melonSuperList中设置一些值。

添加对象类型对象

melonSuperList.add(new Object())

//This is not valid as melonSuperList can be a list of Fruit or Melon.
//Note that Melon itself can be considered as super class of Melon.

添加水果类型对象

melonSuperList.add(new Fruit())

//This is also not valid as melonSuperList can be list of Melon

添加Melon类型对象

melonSuperList.add(new Melon())

//This is valid because melonSuperList can be list of Object, Fruit or Melon and in 
//this entire list we can add Melon type object.

添加西瓜类型对象

melonSuperList.add(new WaterMelon())

//This is also valid because of same reason as adding Melon

综上所述,我们可以在melonSuperList中添加Melon或其子类,并且只读取Object类型的对象。

Super是下界,extends是上界。

根据http://download.oracle.com/javase/tutorial/extra/generics/morefun.html:

解决方法是使用一种形式的 有界通配符我们还没见过 具有下界的通配符。的 语法呢?超级T表示未知 类型是T(或T 本身;记住超类型 关系是自反的)。它是双重的 有界通配符 使用,我们在哪里使用?将T延伸到 表示未知类型 T的子型。