Pri*_*shi 8 java generics collections type-parameter
我对以下两个方法声明感到困惑:
private <U, T extends U> T funWorks(T child, U parent) {
// No compilation errors
}
private <T, U super T> T funNotWorks(T child, U parent) {
// compilation errors
}
Run Code Online (Sandbox Code Playgroud)
以上两者都不应该有效吗?比喻 如果U是T的父,那么T是U的子.那为什么第二个会出现编译错误?
编辑::我认为,T extends T和T super T都是有效的.对 ?
TypeParameter:
TypeVariable TypeBoundopt
TypeBound:
extends TypeVariable
extends ClassOrInterfaceType AdditionalBoundListopt
AdditionalBoundList:
AdditionalBound AdditionalBoundList
AdditionalBound
AdditionalBound:
& InterfaceType
Run Code Online (Sandbox Code Playgroud)
extends或super(JLS#4.5.1):TypeArguments:
< TypeArgumentList >
TypeArgumentList:
TypeArgument
TypeArgumentList , TypeArgument
TypeArgument:
ReferenceType
Wildcard
Wildcard:
? WildcardBoundsopt
WildcardBounds:
extends ReferenceType
super ReferenceType
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
384 次 |
| 最近记录: |