基于Bert F's answer我想解释一下我的理解。
假设我们有 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.
添加 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.
最后让我们尝试添加 WaterMelon 对象
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 as
public class SaltyLemon extends Lemon{}
现在fruitExtendedList 可以是Fruit、Melon、WaterMelon 或SaltyLemon 的列表。
所以,我们的声明
fruitExtendedList.add(new WaterMelon())
也无效。
基本上我们可以说我们不能向fruitExtendedList 写入任何内容。
总结一下List<? extends Fruit>
现在让我们看看
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、WaterMelon 或任何其他对象都无法读取。
但请注意,我们可以读取 Object 类型的实例
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 中设置一些值。
添加Object类型的对象
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.
添加 WaterMelon 类型对象
melonSuperList.add(new WaterMelon())
//This is also valid because of same reason as adding Melon
综上所述,我们可以在melonSuperList中加入Melon或者它的子类,只读Object类型的对象。