Capture Conversion解读
(1)If Ti is a wildcard type argument of the form ?, then Si is a fresh type variable whose upper bound is Ui[A1:=S1,...,An:=Sn] and whose lower bound is the null type.
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public class CaptureExampleUnboundedWildcard<E extends InputStream> {
public void readFromIt(E readFrom) throws IOException{
readFrom.read();
}
public E getSomething(){
return null;
}
public static void main(String [] args) throws Exception{
CaptureExampleUnboundedWildcard<?> ce=new CaptureExampleUnboundedWildcard<BufferedInputStream>();
BufferedInputStream obj=new BufferedInputStream(null);
InputStream i = ce.getSomething(); // ok
ce.readFromIt(obj); // compiler-time error
}
}
这里的CaptureExampleUnboundedWildcard就是G,A1,...An就是<E extends InputStream>,相对应的上/下界声明(U1,..,Un)就是InputStream。
G<T1,...,Tn>是这里的CaptureExampleUnboundedWildcard<?>(在英文中叫a parameterized type,中文译为一个参数化的类型),在调用readFromIt()方法时就需要capture conversion了,也就是为方法中的E推断出G<S1,...,Sn>。
调用readFromIt()方法会报错,Eclipse的提示如下:
The method readFromIt(capture#1-of ?) in the type CaptureExampleUnboundedWildcard<capture#1-of ?> is not applicable for the arguments (BufferedInputStream)
其中有对Ui[A1:=S1,...,An:=Sn]这个表达式的理解。参考阅读:
(1)http://docs.oracle.com/javase/specs/jls/se8/html/jls-1.html#jls-1.3
(2)http://stackoverflow.com/questions/31206947/understanding-captured-conversion
也就是将声明的A1,...An用S1,...Sn来代替,并且新的这个S1,..,Sn的上界为Ui[A1:=S1,...,An:=Sn]。那么如上的例子中ce.readFromIt()方法调用中,实际的类型在new CaptureExampleUnboundedWildcard 时就已经确定为BufferedInputStream,可惜编译器并不保持这个类型变量,而是通过参数化类型声明CaptureExampleUnboundedWildcard<?>和类定义时的类型参数声明CaptureExampleUnboundedWildcard<E extends InputStream>来推断。推断后编译器只能知道新变量的上界为InputStream,具体是InputStream中什么具体的子类型,编译器并不知道,也就不允许放入BufferedInputStream了。
假如在如上类中添加一个方法,如下:
public <X extends InputStream> void readFromIt2(X x) throws IOException{
x.read();
}
调用如上方法然后传入一个BufferedInputStream的实例后,能够正常调用,因为编译器马上推断出了X类型变量的类型为BufferedInputStream,并且满足上界约束条件。
(2)If Ti is a wildcard type argument of the form ? extends Bi, then Si is a fresh type variable whose upper bound is glb(Bi, Ui[A1:=S1,...,An:=Sn]) and whose lower bound is the null type.
public class CaptureExampleUpperBoundedWildcard<E extends InputStream> {
public void readFromIt(E readFrom) throws IOException{
readFrom.read();
}
public static void main(String [] args) throws Exception{
//The ce is passed only ? extends Serializable as a type argument. Hence the capture converted type has an upper
//bound same as the original bound during the declaration, which is InputStream
//Note that it is not possible to create an Object of wildcard type. That is a compile
//time error
CaptureExampleUpperBoundedWildcard<? extends BufferedInputStream> ce=new CaptureExampleUpperBoundedWildcard<BufferedInputStream>();
}
}
It is a compile-time error if, for any two classes (not interfaces) Vi and Vj, Vi is not a subclass of Vj or vice versa.
这里BufferedInputStream是InputStream类的子类,否则会出现编译错误。
(3)If Ti is a wildcard type argument of the form ? super Bi, then Si is a fresh type variable whose upper bound is Ui[A1:=S1,...,An:=Sn] and whose lower bound is Bi.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public class CaptureExampleLowerBoundedWildcard<E extends InputStream> {
public void readFromIt(E readFrom) throws IOException {
readFrom.read();
}
public E getSomething(){
return null;
}
public static void main(String[] args) throws Exception {
CaptureExampleLowerBoundedWildcard<? super FilterInputStream> ce = new CaptureExampleLowerBoundedWildcard<InputStream>();
BufferedInputStream obj = new BufferedInputStream(new FileInputStream("somefile"));
// Note that subclasses of the lower bound are allowed in method
// invocation due normal method invocation conversion
ce.readFromIt(obj); // ok
InputStream b = ce.getSomething(); // ok
}
}
通过a parameterized type,也就是CaptureExampleLowerBoundedWildcard<? super FilterInputStream>与类定义时的类型参数声明CaptureExampleLowerBoundedWildcard<E extends InputStream>可知,新的类型变量下界为FilterInputStream,而上界为InputStream。那么满足这个条件的都可以,在new CaptureExampleLowerBoundedWildcard时传递了InputStream和FilterInputStream,而不能使用BufferedInputStream。
值得注意的是,ce.readFromIt(obj)中,这个obj是bufferedInputStream,也就是FilterInputStream的直接子类,按理说不满足如上的上下界,这是由于发生了method invocation conversion,具体就是a widening reference conversion转换,导致obj也满足了限定条件。
interface IA {}
class B implements IA {}
class Test6<T extends IA> {
public void test() {
Test6<? super B> x = new Test6<B>(); // B不实现IA会报错
}
}
(4)Otherwise, Si = Ti
public class CaptureExample<E, F extends E> {
public void copy(List<E> toGet, List<F> copyFrom){
for(F x:copyFrom){
toGet.add(x);
}
}
public static void main(String [] args){
List<Object> aList=new ArrayList<Object>();
List<String> copyFrom=new ArrayList<String>();
copyFrom.add("Hi");
CaptureExample<Object, String> ce=new CaptureExample<Object, String>();//Here is where the capture conversion takes place
ce.copy(aList, copyFrom);
System.out.println(aList);
}
}
重新解读通配符:
e.g 1
public class CaptureExampleUnboundedWildcard<E extends InputStream> {
// 方法形式参数表示设置值
public void set(E readFrom) throws IOException{
readFrom.read();
}
// 返回值为类型参数,表示取值
public E get(){
return null;
}
public static void main(String [] args) throws Exception{
CaptureExampleUnboundedWildcard<?> ce = new CaptureExampleUnboundedWildcard<InputStream>();
//The method set(capture#1-of ?) in the type CaptureExampleUnboundedWildcard<capture#1-of ?>
//is not applicable for the arguments (BufferedInputStream)
BufferedInputStream bis = null;
ce.set(bis);
//The method set(capture#2-of ?) in the type CaptureExampleUnboundedWildcard<capture#2-of ?>
//is not applicable for the arguments (InputStream)
InputStream is = null;
ce.set(is);
ce.set(null);
Object o1 = ce.get();
InputStream o2 = ce.get();
// Type mismatch: cannot convert from capture#6-of ? to FilterInputStream
FilterInputStream o3 = ce.get();
}
}
从上面的实例可以看出,通配符为?时,只能设置null值,但是可以取值。
e.g 2
interface IA{}
class A{}
class B{}
public class CaptureExampleUpperBoundedWildcard<E extends A> {
// 方法形式参数表示设置值
public void set(E readFrom) throws IOException{
}
// 返回值为类型参数,表示取值
public E get(){
return null;
}
public static void main(String [] args) throws Exception{
//Bound mismatch: The type ? extends B is not a valid substitute for the bounded parameter <E extends A>
//of the type CaptureExampleUpperBoundedWildcard<E>
//CaptureExampleUpperBoundedWildcard<? extends IA> ce = new CaptureExampleUpperBoundedWildcard();
CaptureExampleUpperBoundedWildcard<? extends IA> ce = new CaptureExampleUpperBoundedWildcard();
// 当为<? extends Serializable>时不能存入任何元素,除了null值
ce.set(null); // 在方法的参数中就相当于给设置值,而在方法的返回中就相当于获取值
Object o1 = ce.get();
A o2 = ce.get();
IA o3 = ce.get();
}
}
从上面的实例可以看出,通配符为? extends X时,只能设置null值,但是可以取值。
声明中的E extends Y 与 通配符声明 ? extends X之间的关系如下:
(1)X与Y有一个为接口时,则相互之间没有限制。
(2)当X与Y都为类时,两个类之间必须有父子关系。抽象类也看作类
e.g 3
class SubBufferedInputStream extends BufferedInputStream{
public SubBufferedInputStream(InputStream in) {
super(in);
}
}
public class CaptureExampleLowerBoundedWildcard<E extends FilterInputStream> {
// 方法形式参数表示设置值
public void set(E readFrom) throws IOException{
readFrom.read();
}
// 返回值为类型参数,表示取值
public E get(){
return null;
}
public static void main(String [] args) throws Exception{
CaptureExampleLowerBoundedWildcard<? super BufferedInputStream> ce=new CaptureExampleLowerBoundedWildcard<FilterInputStream>();
//Bound mismatch: The type ? super InputStream is not a valid substitute for
//the bounded parameter <E extends FilterInputStream> of the type CaptureExampleLowerBoundedWildcard<E>
// CaptureExampleLowerBoundedWildcard<? super InputStream> ce = new CaptureExampleLowerBoundedWildcard<FilterInputStream>();
//Now we can call this without an error, because the capture converted type does have a lower bound.
//Note that subclasses of the lower bound are allowed in method invocation due normal method invocation conversion
BufferedInputStream obj= null;
ce.set(obj);
SubBufferedInputStream sis = null;
ce.set(sis);
//The method set(capture#3-of ? super BufferedInputStream) in the type
//CaptureExampleLowerBoundedWildcard<capture#3-of ? super BufferedInputStream> is
//not applicable for the arguments (FilterInputStream)
FilterInputStream fis = null;
ce.set(fis); // error
FilterInputStream o1 = ce.get();
// Type mismatch: cannot convert from capture#5-of ? super BufferedInputStream to SubBufferedInputStream
SubBufferedInputStream o2 = ce.get(); // error
// Type mismatch: cannot convert from capture#6-of ? super BufferedInputStream to BufferedInputStream
BufferedInputStream o3 = ce.get(); // error
}
}
从上面的实例可以看出,通配符为? super X时,可以设置X及其子类,取值时能接收的最精确的类型为声明时E extends Y的Y类型。
X与Y不管是接口还是类必须要形成父子关系。
当声明为E,而通配符为?,? extends X或者? super X时,会是什么情况呢?也就是? extends X或者 ? super X时的类型X无限制。取值时只能以Object来接收。
参考文献:
(1)http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1
(2)http://www.geekyarticles.com/2011/11/java-generics-capture-conversion.html
(3)https://www.ibm.com/developerworks/java/library/j-jtp04298/index.html
Capture Conversion解读的更多相关文章
- Thinking in Java——笔记(15)
Generics The term "generic" means "pertaining or appropriate to large groups of class ...
- java泛型小问题
几年前当Java5还未正式发布的时候,看到过一些人写的介绍Tiger中的新特性,当时对我第一感觉冲击最大的就是泛型(generics)和注释(annotation),因为它们直接影响了我们编码的语法习 ...
- javac后期需要重点阅读的类
(1)Annotate (300行) Enter annotations on symbols. Annotations accumulate in a queue,which is processe ...
- javac的泛型
?:在实例化对象的时候,不确定泛型参数的具体类型时,可以使用通配符进行对象定义. (1)?表示通配符,通配符 与 T 的区别 T:作用于模板上,用于将数据类型进行参数化,不能用于实例化对象. publ ...
- Javac之glb与lub
5.1.10. Capture Conversion Let G name a generic type declaration (§8.1.2, §9.1.2) with n type parame ...
- Types方法之isSameType-isSuperType-isSubType
4.isSameType() 方法 /** * Is t the same type as s? */ public boolean isSameType(Type t, Type s) { retu ...
- Types方法之isCastable-isConvertible
5. Conversions and Promotions 5.1. Kinds of Conversion 5.1.1. Identity Conversion 5.1.2. Widening Pr ...
- Android MVP模式 谷歌官方代码解读
Google官方MVP Sample代码解读 关于Android程序的构架, 当前(2016.10)最流行的模式即为MVP模式, Google官方提供了Sample代码来展示这种模式的用法. Repo ...
- 2014百度之星资格赛 1001:Energy Conversion(水题,逻辑题)
Energy Conversion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- mysql 统计一个字符在字符串中出现的次数
CREATE FUNCTION `str_pcount`(str varchar(255),p varchar(255)) RETURNS int(11)BEGIN #统计一个字符在字符串中出 ...
- 深水划水队项目---七天冲刺之day4
由于有一名队员回家了,所以今天的站立式会议改成微信群线上讨论 工作进度: 1.游戏界面进一步美化中. 2.游戏基本功能已经初步实现. 3.计划今天内商讨出如何实现游戏中的难度选择功能与道具功能. 工作 ...
- [label][PHP-Security]PHP Security Program
本文是通过阅读http://www.nowamagic.net/中的PHP安全变成专题,同时结合个人的一点点开发经验组合而成的. 如果你需要看原文,可以直接访问http://www.nowamagic ...
- java 传参数时 类型后跟 3个点 "..." 的意义
对照代码和运行结果便知"..." 的意义 import java.util.ArrayList; public class StringDemo { public static v ...
- SqlerMonitor-复制
在复制系统中因为一些配置上失误和人为的失误操作导致复制堵塞,Sqler Monitor 新增加了分析复制延迟邮件,配合复制错误监控邮件和延迟邮件,和复制元数据采集 可以在第一时间准确定位到问题,适合大 ...
- 调整Linux最大打开文件数
#!/bin/bash ## 文件数限制 ulimit -n ulimit -Sn ulimit -Hn ## fs.nr_open,进程级别 ## fs.file-max,系统级别 ## 最大文件描 ...
- Handler运行机制
https://blog.csdn.net/u012827296/article/details/51236614
- 浅谈由管理者角色引出的B端产品设计思考点
本文来自网易云社区 作者:任琼瑶 最近一直都在持续跟进云课堂B端的交互设计,在此期间,多多少少遇到了一些不曾遇到过的问题,虽然同是做产品设计,但B端和C端产品的确存在很多不同之处. 首先,当我们在做C ...
- python网络编程--线程(锁,GIL锁,守护线程)
1.线程 1.进程与线程 进程有很多优点,它提供了多道编程,让我们感觉我们每个人都拥有自己的CPU和其他资源,可以提高计算机的利用率.很多人就不理解了,既然进程这么优秀,为什么还要线程呢?其实,仔细观 ...
- case when then end 用法
select type,numbers,case monthst when 1 then '一月'when 2 then '二月'when 3 then '三月'else '其他月'end month ...