Java 有界通配符类型
我需要定义一个泛型类,并且类型参数必须是枚举。我认为它应该看起来像
public class <T> MyClass<T extends Enum<T>> {
}
但我似乎无法弄清楚确切的语法。我应该提到,我需要一种方法来引用它实例化的类型(在 MyClass 内)。
谢谢!
I need to define a generic class, and the type parameter must be an enum. I think it should look something like
public class <T> MyClass<T extends Enum<T>> {
}
But I can't seem to figure out the exact syntax. I should mention that I need a way to refer to the type (within MyClass) that it is instantiated with.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然批准的答案在语法上看起来是正确的,但这样的声明的场景是什么?您是否正在尝试编写一些可以对 任何
enum
定义的类型进行操作的类,例如java.util.EnumSet
或java.util.EnumMap
?这是一种不寻常的安排,因此请确保您确实需要它以满足您的要求。While the approved answer looks syntactically correct, what's the scenario for such a declaration? Are you trying to write some class that can operate on any
enum
-defined type, likejava.util.EnumSet
orjava.util.EnumMap
? It's an unusual arrangement, so be sure you really need it in order to meet your requirements.