问题描述
可以将一个泛型参数绑定为:
public< T super Object> void someMethod(T t);
是否有这样一个绑定的有效用法?
根据JLS 类型参数(< T
TypeParameter:
TypeVariable TypeBound *
TypeBound:
extends TypeVariable
extends ClassOrInterfaceType AdditionalBoundList *
AdditionalBoundList:
AdditionalBound AdditionalBoundList
AdditionalBound
AdditionalBound:
&接口类型
* =可选
TypeBound
,仅指定 extends
的用法。
不幸的是,未指定使用 super
的下限类型参数。下限仅在通配符用法中指定()
好的问题,让我在JLS中挖掘,我不知道为什么没有实现在java中,它只是没有指定。
It is possible to code a generic parameter bound as:
public <T super Object> void someMethod(T t);
Is there a valid usage of such a bound?
According to the JLS http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.4 a type parameter (The < T extends Object > term) consists out of:
TypeParameter:
TypeVariable TypeBound*
TypeBound:
extends TypeVariable
extends ClassOrInterfaceType AdditionalBoundList*
AdditionalBoundList:
AdditionalBound AdditionalBoundList
AdditionalBound
AdditionalBound:
& InterfaceType
* = optional
You see the TypeBound
, there is only the usage of extends
specified.Unfortunately, a lower bound type parameter, using super
, is not specified. A lower bound is only specified in the wildcard usage (JLS#4.5.1)
Good question, that got me digging in the JLS, and I don't know why this not implemented in java, it is just not specified.
这篇关于对象的泛型下界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!