是否可以将泛型添加到枚举值?包含示例伪代码
是否可以将泛型添加到枚举值?
与此伪代码类似的东西:
public enum MyEnum<T> {
VALUE1<String>,
VALUE2<Boolean>;
public T get() {
return (T) AnotherSystem.get(this); // Where AnotherSystem.get returns an Object
}
}
我刚刚写错了,还是这根本不可能?
我还需要哪些其他选项才能使 get() 返回特定的泛型类型 (T)
Is it possible to add generics to enum values?
Something similar to this pseudo code:
public enum MyEnum<T> {
VALUE1<String>,
VALUE2<Boolean>;
public T get() {
return (T) AnotherSystem.get(this); // Where AnotherSystem.get returns an Object
}
}
Have I just written this incorrectly, or is this not possible at all?
What other options do I have to make get() return a specific generic type (T)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要
将类型与枚举关联起来,它可以是属性
You can do
If you need to associate a type with an enum, it can be a property
不,我不相信这样的事情是可能的。如果您查看JLS 的第 8.9 节 ,在指定枚举声明的地方,那里没有任何关于泛型的内容:
将其与 第 8.1 节,其中规范的一部分是类型涉及的参数。
No, I don't believe anything like this is possible. If you look at section 8.9 of the JLS, where enum declarations are specified, there's nothing about generics there:
Compare it with the "normal" class declaration in section 8.1, where part of the spec is the type parameters involved.