是不是<U,T延伸U>和<T,U super T>相同?

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 TT super T都是有效的.对 ?

ass*_*ias 7

  • 类型参数(您的示例)只能使用extends(JLS#4.4):
TypeParameter:
    TypeVariable TypeBoundopt

TypeBound:
    extends TypeVariable
    extends ClassOrInterfaceType AdditionalBoundListopt

AdditionalBoundList:
    AdditionalBound AdditionalBoundList
    AdditionalBound

AdditionalBound:
    & InterfaceType
Run Code Online (Sandbox Code Playgroud)
  • 通配符可以使用extendssuper(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)