Covariance and Contravariance in Java
2016-06-02 23:53
537 查看
extends
The wildcard declaration of List<? extends Number> foo3means 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 guarenteed to read from
List foo3:
You can read a
Numberbecause
any of the lists that could be assigned to
foo3contain
a
Numberor
a subclass of
Number.
You can't read an
Integerbecause
foo3could
be pointing at a
List<Double>.
You can't read a
Doublebecause
foo3could
be pointing at a
List<Integer>.
Writing - Given the above possible assignments, what type of object could you add to
List foo3that would be legal for all the above possible
ArrayListassignments:
You can't add an
Integerbecause
foo3could
be pointing at a
List<Double>.
You can't add a
Doublebecause
foo3could
be pointing at a
List<Integer>.
You can't add a
Numberbecause
foo3could
be pointing at a
List<Integer>.
You can't add any object to
List<? extends T>because you can't guarantee what kind of
Listit
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
Tor
subclass of
T.
super
Now consider List <? super T >.
The wildcard declaration of
List<? super Integer> foo3means 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:
You aren't guaranteed an
Integerbecause
foo3could
be pointing at a
List<Number>or
List<Object>.
You aren't guaranteed an
Numberbecause
foo3could
be pointing at a
List<Object>.
The only guarantee is that you will get an instance of an
Objector
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 foo3that would be legal for all the above possible
ArrayListassignments:
You can add an
Integerbecause
an
Integeris
allowed in any of above lists.
You can add an instance of a subclass of
Integerbecause
an instance of a subclass of
Integeris
allowed in any of the above lists.
You can't add a
Doublebecause
foo3could
be pointing at a
ArrayList<Integer>.
You can't add a
Numberbecause
foo3could
be pointing at a
ArrayList<Integer>.
You can't add a
Objectbecause
foo3could
be pointing at a
ArrayList<Integer>.
Link : http://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java
Refer to this one for details : http://www.codeproject.com/Articles/899319/Comparing-covariance-contravariance-rules
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树