用于枚举类型的约束泛型类型来实现某些接口

时间:2021-04-22 16:38:12

I have enum that implement MyInterface. While making other class using that enum I want to constrain the enumClz to be class that has implemented MyInterface.

我有实现MyInterface的枚举。在使用该枚举创建其他类时,我想将enumClz约束为已实现MyInterface的类。

So I describe signature to be "T extends Enum< T extends MyInterface>" at generic type declaration.

所以我在泛型类型声明中将签名描述为“T extends Enum ”。

public <T extends Enum< T extends MyInterface>> C1( Class<T> enumClz) {
    for (T anEnumConst : enumClz.getEnumConstants()) {
        //....process
    }
}

What surprised me is the IDE say it is "unexpected bound" at "T extends MyInterface". I don't know what it means by such two word error message, Any solution about this?

让我感到惊讶的是IDE说它在“T extends MyInterface”中“意外受限”。我不知道这两个字的错误信息意味着什么,有什么解决方案吗?


And by the way, out of curious I have an odd question though not really important. Can an enum type T be equivalent to the following infinite loop

顺便说一下,出于好奇,我有一个奇怪的问题,虽然不是很重要。枚举类型T可以等效于以下无限循环

<T extends Enum< T extends Enum<T extends<....>>>> ?

>>>? 延伸enum>

1 个解决方案

#1


9  

Declare the following instead:

改为声明以下内容:

public <T extends Enum<T> & MyInterface> C1(Class<T> enumClz)

Here, we're declaring T to have multiple upper bounds, which is possible for type parameters.

这里,我们声明T有多个上限,这对于类型参数是可能的。

The declaration <T extends Enum<T extends MyInterface>> is invalid syntax because T must be bounded with a concrete type, but the T extends MyInterface in the type argument for Enum is trying to add more information about T when it's already been declared.

声明 >是无效的语法,因为T必须以具体类型为界,但是T的扩展MyInterface在Enum的类型参数中试图在已经声明时添加有关T的更多信息。

Note also that a class type must always come first when declaring multiple bounds. A declaration of <T extends MyInterface & Enum<T>> is also invalid syntax.

还要注意,在声明多个边界时,类类型必须始终排在第一位。声明 >也是无效的语法。

And by the way, out of curious I have an odd question though not really important. Can an enum type T be equivalent to the following infinite loop

顺便说一下,出于好奇,我有一个奇怪的问题,虽然不是很重要。枚举类型T可以等效于以下无限循环

<T extends Enum< T extends Enum<T extends<....>>>> ?

>>>? 延伸enum>

The declaration T extends Enum<T> is already "infinite" in that it's recursive. The same T that is being declared is given as a type argument for its upper bound - a type parameter's scope includes its own declaration.

声明T扩展Enum 已经是“无限”,因为它是递归的。声明的相同T作为其上限的类型参数给出 - 类型参数的范围包括其自己的声明。

More information:

#1


9  

Declare the following instead:

改为声明以下内容:

public <T extends Enum<T> & MyInterface> C1(Class<T> enumClz)

Here, we're declaring T to have multiple upper bounds, which is possible for type parameters.

这里,我们声明T有多个上限,这对于类型参数是可能的。

The declaration <T extends Enum<T extends MyInterface>> is invalid syntax because T must be bounded with a concrete type, but the T extends MyInterface in the type argument for Enum is trying to add more information about T when it's already been declared.

声明 >是无效的语法,因为T必须以具体类型为界,但是T的扩展MyInterface在Enum的类型参数中试图在已经声明时添加有关T的更多信息。

Note also that a class type must always come first when declaring multiple bounds. A declaration of <T extends MyInterface & Enum<T>> is also invalid syntax.

还要注意,在声明多个边界时,类类型必须始终排在第一位。声明 >也是无效的语法。

And by the way, out of curious I have an odd question though not really important. Can an enum type T be equivalent to the following infinite loop

顺便说一下,出于好奇,我有一个奇怪的问题,虽然不是很重要。枚举类型T可以等效于以下无限循环

<T extends Enum< T extends Enum<T extends<....>>>> ?

>>>? 延伸enum>

The declaration T extends Enum<T> is already "infinite" in that it's recursive. The same T that is being declared is given as a type argument for its upper bound - a type parameter's scope includes its own declaration.

声明T扩展Enum 已经是“无限”,因为它是递归的。声明的相同T作为其上限的类型参数给出 - 类型参数的范围包括其自己的声明。

More information: