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<....>>>>
?
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.
声明
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
More information:
- Bounded Type Parameters
- Can I use a type parameter as part of its own bounds?
- How do I decrypt "
Enum<E extends Enum<E>>
"?
有界类型参数
我可以使用类型参数作为其自身边界的一部分吗?
如何解密“Enum
#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.
声明
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
More information:
- Bounded Type Parameters
- Can I use a type parameter as part of its own bounds?
- How do I decrypt "
Enum<E extends Enum<E>>
"?
有界类型参数
我可以使用类型参数作为其自身边界的一部分吗?
如何解密“Enum