阅读The Java® Language Specification需要知道的术语
2017-01-19 10:33
330 查看
15.12.2.7. Inferring Type Arguments Based on Actual Arguments
U << V indicates that type U is convertible to type V by method invocation conversion (§5.3), and U >> V indicates that type V is convertible to type U by method invocation conversion.例如interface A<T>{}中的T。class B<T extends InputStream>中的T都是a type variable
public class Test3<X> { // A extends Object和B extends Comparable为R1,R2,Object与Comparable为B1与B2。 List<? extends A>与B为F1,F2 public <A extends Object, B extends Comparable,C extends X> void test(List<? extends A> x, B b) { } }
如上解释了TypeBound: extends TypeVariable形式。方法test中的类型参数中C extends X中的X为Type Variable形式。
关于构造函数和方法的类型擦除,举个例子:
public class TestGenericErasure<T extends InputStream> { public <X extends Serializable&Comparable<T>>void test01(T t,X x){ } }
擦除后变为:
public class TestGenericErasure { public TestGenericErasure() { super(); } public void test01(InputStream t, Serializable x) { } }
可以看到方法中所有的type parameter全部被擦除。一般方法中的类型参数、返回类型与形式参数类型都会进行泛型擦除。
下面来看理解这句话:
The erasure of a type variable (§4.4) is the erasure of its leftmost bound.
举个例子,如下:
public class Generic<T extends Object & Appendable & AutoCloseable> { T t; T method() throws Exception { t.close(); char c='\u0000'; t.append(c); return t; } public <T> T method2(T t) { return t; } }
那么T将全部替换为Object,最后泛型擦除后的结果如下:
public class Generic{ public Test() { super(); } Object t; Object method() throws Exception { ((AutoCloseable)t).close(); // 强制类型转换 char c = '\u0000'; ((Appendable)t).append(c); // 强制类型转换 return t; } public Object method2(Object t) { return t; } }
a variable arity method与a fixed arity method
public class Test1 { public void m(Object ...x){ System.out.println("a variable arity method"); } public void m(Object x){ System.out.println("a fixed arity method"); } public static void main(String[] args) { new Test1().m(null); // 打印为a variable arity method } }
if and only if 会被写为iff
Covariance, Invariance and Contravariance 概念解析
At heart, these terms describe how the subtype relation is affected by type transformations. That is, if
Aand
Bare types,
fis a type transformation, and ≤ the subtype relation (i.e.
A ≤ Bmeans that
Ais a subtype of
B), we have
fis covariant if
A ≤ Bimplies that
f(A) ≤ f(B)
fis contravariant if
A ≤ Bimplies that
f(B) ≤ f(A)
fis invariant if neither of the above holds
如某些泛型是invariant,因为String≤ Object,但是List<String>与List<Object>没有关系
数组是covariant,因为String≤Object,有String[]≤Object[]
class AA{ public Integer[] aa(){ return null; } public String[] aa(int a){ return null; } public String[] bb(int a){ return null; } } class BB extends AA{ public String[] bb(){ return null; } }
有通配符的泛型是contravariant,因为String≤Number,有List<? super Number> ≤ List<? super String>
参阅文章:
(1)http://stackoverflow.com/questions/8481301/covariance-invariance-and-contravariance-explained-in-plain-english/42239324#42239324
(2)http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ201
相关文章推荐
- 阅读The Java® Language Specification需要知道的英文单词
- java日志,需要知道的几件事
- java中的gui 单击右上角关闭按钮时所需要知道的
- 产品经理所需要知道的专业术语
- 手写java服务器需要知道的一些函数
- java日志,需要知道的几件事(commons-logging,log4j,slf4j,logback)
- Java语言规范——每个Java开发人员都需要认真阅读的书籍,包含下载地址
- 阅读hadop源码需要搞清楚的java编程知识
- java日志,需要知道的几件事
- Java日志,需要知道的几件事(commons-logging,log4j,slf4j,logback)
- Java日志,需要知道的几件事(commons-logging,log4j,slf4j,logback)
- 手工编译、运行JavaSE项目,且此项目需要第三方jar包支持,且文件中有打包。 分类: java 2010-03-27 10:37 1057人阅读 评论(0) 收藏
- 阅读QtCreator-需要知道的qmake
- 天津网站建设需要知道的开源软件(Java篇)
- java关于==需要知道的一些东西
- java日志,需要知道的几件事
- java日志,需要知道的几件事
- 一些你需要知道的Java编程代码规范
- 你需要知道的10位Java开发牛人
- java web服务之RESTful需要知道的annotation