小编典典
super绑定命名类型参数(例如)而不是通配符(例如 super T>)是非法的,这仅仅是因为即使允许,它也不会执行你希望的操作,因为既然这Object是super所有引用类型的最终值,一切是Object,实际上有没有约束。
在你的特定示例中,由于任何引用类型的数组都是Object[](通过Java数组协方差),因此可以 S[] toArray(S[] a)在编译时用作(如果这样的绑定是合法的)自变量,并且不会ArrayStoreException在运行时阻止- 时间。
你要提出的建议是:
List integerList;
并鉴于此假设 super界限toArray:
S[] toArray(S[] a) // hypothetical! currently illegal in Java
编译器只应允许以下内容进行编译:
integerList.toArray(new Integer[0]) // works fine!
integerList.toArray(new Number[0]) // works fine!
integerList.toArray(new Object[0]) // works fine!
并且没有其他数组类型参数(因为Integer只有这3种类型作为super)。也就是说,你正在尝试阻止此编译:
integerList.toArray(new String[0]) // trying to prevent this from compiling
因为,根据你的说法,String不是super的Integer。但是,Object是的a super和Integer,并且a String[]是an Object[],因此即使假设你可以做,编译器仍会让上述内容进行编译!
因此,以下内容仍将进行编译(就像它们现在所采用的方式一样),并且ArrayStoreException在运行时无法通过使用泛型类型边界进行的任何编译时检查来阻止:
integerList.toArray(new String[0]) // compiles fine!
// throws ArrayStoreException at run-time
泛型和数组不会混合使用,这是它显示的众多地方之一。
一个非数组的例子
再说一次,假设你具有以下通用方法声明:
void add(T number) // hypothetical! currently illegal in Java
并且你具有以下变量声明:
Integer anInteger
Number aNumber
Object anObject
String aString
你的意图(如果合法)是它应该允许add(anInteger),并且add(aNumber)当然可以add(anObject),但不允许add(aString)。好吧,String是Object,所以add(aString)仍然可以编译。
2020-03-09