PECS - Pruducer extends, Consumer super
Only applies to input parameters.
Do not use wildcard type for return value - doesn't make it more flexible!
List<String> is a subtype of List<? extends Object>
List<Object> is a subtype of List<? super String>
eg.
pushAll(Collection<? extends E> src);
- src is an E producer; you can push Long to Collection<Number>
popAll(Collection<? super E> dst);
- dst is an E consumer; you can pop Numbers to Collection<Object>
Both produces and consumes use T.
Neither produces or consumes use ?.
No comments:
Post a Comment