3 回答
TA贡献1802条经验 获得超10个赞
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>
Parent
List<Parent>
List<Child>
List<GrandChild>
List<T>
T
Parent
List<T>
Parent
T
TA贡献1812条经验 获得超5个赞
type L<T> T get(); void set(T);
P
C1, C2 ... Cn
P
Ci
)
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()
X
void set(X)
void set(Ci)
Ci
set()
L<? extends P>
.
L<? super P>
set(P)
get()
Si
P
, 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)
添加回答
举报