为什么这段代码不能编译(Parent
是一个接口)?
List<? extends Parent> list = ...
Parent p = factory.get(); // returns concrete implementation
list.set(0, p); // fails here: set(int, ? extends Parent) cannot be applied to (int, Parent)
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 45
为了安全起见,这样做.想象一下,如果它有效:
List<Child> childList = new ArrayList<Child>();
childList.add(new Child());
List<? extends Parent> parentList = childList;
parentList.set(0, new Parent());
Child child = childList.get(0); // No! It's not a child! Type safety is broken...
Run Code Online (Sandbox Code Playgroud)
意思List<? extends Parent>
是"这是一个扩展的某种类型的列表Parent
.我们不知道哪种类型 - 它可能是a List<Parent>
,a List<Child>
或a List<GrandChild>
." 这使得它的安全抓取任何物品出来的的List<T>
API和转换T
到Parent
,但它不是安全地调用中的List<T>
API从转换Parent
到T
......因为转换可能是无效的.
Boz*_*zho 16
List<? super Parent>
Run Code Online (Sandbox Code Playgroud)
PECS - "制作人 - 扩展,消费者 - 超级".你List
是Parent
对象的消费者.