extends
the wildcard declaration of list<? extends number> foo3
means that any of these are legal assignments:
list<? extends number> foo3 = new arraylist<number>(); // number "extends" number (in this context) list<? extends number> foo3 = new arraylist<integer>(); // integer extends number list<? extends number> foo3 = new arraylist<double>(); // double extends number
reading - given the above possible assignments, what type of object are you guaranteed to read from list foo3
:
number
because any of the lists that could be assigned to foo3
contain a number
or a subclass of number
.integer
because foo3
could be pointing at a list<double>
.double
because foo3
could be pointing at a list<integer>
.writing - given the above possible assignments, what type of object could you add to list foo3
that would be legal for all the above possible arraylist
assignments:
integer
because foo3
could be pointing at a list<double>
.double
because foo3
could be pointing at a list<integer>
.number
because foo3
could be pointing at a list<integer>
.you can't add any object to list<? extends t>
because you can't guarantee what kind of list
it is really pointing to, so you can't guarantee that the object is allowed in that list
. the only "guarantee" is that you can only read from it and you'll get a t
or subclass of t
.
super
now consider list <? super t>
.
the wildcard declaration of list<? super integer> foo3
means that any of these are legal assignments:
list<? super integer> foo3 = new arraylist<integer>(); // integer is a "superclass" of integer (in this context) list<? super integer> foo3 = new arraylist<number>(); // number is a superclass of integer list<? super integer> foo3 = new arraylist<object>(); // object is a superclass of integer
reading - given the above possible assignments, what type of object are you guaranteed to receive when you read from list foo3
:
integer
because foo3
could be pointing at a list<number>
or list<object>
.number
because foo3
could be pointing at a list<object>
.object
or subclass of object
(but you don't know what subclass).writing - given the above possible assignments, what type of object could you add to list foo3
that would be legal for all the above possible arraylist
assignments:
integer
because an integer
is allowed in any of above lists.integer
because an instance of a subclass of integer
is allowed in any of the above lists.double
because foo3
could be pointing at an arraylist<integer>
.number
because foo3
could be pointing at an arraylist<integer>
.object
because foo3
could be pointing at an arraylist<integer>
.remember pecs: "producer extends, consumer super".
"producer extends" - if you need a list
to produce t
values (you want to read t
s from the list), you need to declare it with ? extends t
, e.g. list<? extends integer>
. but you cannot add to this list.
"consumer super" - if you need a list
to consume t
values (you want to write t
s into the list), you need to declare it with ? super t
, e.g. list<? super integer>
. but there are no guarantees what type of object you may read from this list.
if you need to both read from and write to a list, you need to declare it exactly with no wildcards, e.g. list<integer>
.
note . note how the source list src
(the producing list) uses extends
, and the destination list dest
(the consuming list) uses super
:
public class collections { public static <t> void copy(list<? super t> dest, list<? extends t> src) { for (int i = 0; i < src.size(); i++) dest.set(i, src.get(i)); } }
also see how can i add to list<? extends number> data structures?
https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java
https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super
如对本文有疑问, 点击进行留言回复!!
HashMap、Hashtable、ConcurrentHashMap三者间的异同
解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题
多线程、同步工作原理、死锁案例、Lock接口、线程的生命周期的讲解及实现
网友评论