您的位置:首页 > 编程语言 > Java开发

阅读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
A
and
B
are types,
f
is a type transformation, and ≤ the subtype relation (i.e.
A ≤ B
means that
A
is a subtype of
B
), we have

f
is covariant if
A ≤ B
implies that
f(A) ≤ f(B)


f
is contravariant if
A ≤ B
implies that
f(B) ≤ f(A)


f
is 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

 

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: