根据Java docs,以下代码应该导致编译错误:
import java.util.*;
public class GenericTest1 {
// Add T-array of objects to collection<T>
static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
for (T o : a) {
c.add(o);
}
}
public static void main( String[] args ) {
Number[] na = new Number[100];
Collection<Number> cn = new ArrayList<Number>();
// This should work and does
fromArrayToCollection( na, cn );
Collection<String> cs = new ArrayList<String>();
// This should fail to copile and does
fromArrayToCollection( na, cs );
}
}
它确实:
GenericTest1.java:25: error: method fromArrayToCollection in class GenericTest1 cannot be applied to given types;
fromArrayToCollection( na, cs );
^
required: T[],Collection<T>
found: Number[],Collection<String>
reason: inference variable T has incompatible bounds
equality constraints: String
lower bounds: Number
where T is a type-variable:
T extends Object declared in method <T>fromArrayToCollection(T[],Collection<T>)
但是,这可以完美地编译和运行。
public class GenericTest2 {
// Test for equality of two objects of type T
static <T> boolean testEquality(T first, T second ) {
return first.equals( second );
}
public static void main( String[] args ) {
// Should work
System.out.println( testEquality( "One", "One" ) );
// Shouldn't this refuse to compile ?
System.out.println( testEquality( "One", 1 ) );
// Shouldn't this refuse to compile ?
Number one = new Integer( 1 );
System.out.println( testEquality( "One", one ) );
}
}
输出是:
true
false
false
任何人都可以解释原因吗?
答案 0 :(得分:2)
它有效,因为one
(Number
)和"One"
(String
)都是Object
,1
({{ 1}}由于autoboxing)和Integer
("One"
)。因此String
被评估为T
,等于被调用并返回Object
。
它不适用于false
(及其他泛型)because a Collection<String>
can not be cast to a Collection<Object>
答案 1 :(得分:1)
testEquality( "One", 1 )
作为自动装箱的结果,1
此处将转换为Integer(1)
,这是一个对象。 String("One")
和Integer(1)
都从.equals
继承了Object
函数,因此可以无误地编译。
答案 2 :(得分:0)
在第二次测试中,p=&a;
只会被装入(int*)
实例,该实例也是1
的实例。