3 回答
TA贡献1876条经验 获得超7个赞
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...
List<? extends Parent>ParentList<Parent>List<Child>List<GrandChild>List<T>TParentList<T>ParentT
TA贡献1895条经验 获得超7个赞
type L<T> T get(); void set(T);
PC1, C2 ... CnPCi)
L<C1>, L<C2> ... L<Cn>
type L_Ci_ Ci get(); void set(Ci);
L<Ci> oi = ...;L<Cj> oj = oi; // doesn't compile. L<Ci> and L<Cj> are not compatible types.
L<? extends P>L<Ci>
L<Ci> oi = ...;L<? extends P> o = oi; // ok, assign subtype to supertype
L<? extends P>
type L<? extends P> P get();
L<Ci>Ci get()P get()
set()Xvoid set(X)void set(Ci)Ciset()L<? extends P>.
L<? super P>set(P)get()SiP, L<? super P>L<Si>.
type L<? super P> void set(P);type L<Si> Si get(); void set(Si);
set(Si)set(P)set(P)set(Si)
添加回答
举报
