是否可以使用方法创建一个空的 Java 枚举类型?
我可以创建一个空的 Java 枚举类型,但是当我想添加一些方法时:我收到“语法错误”(进入 Eclipse)。 我没有找到关于此的官方文档,所以我的问题是:这是否是不可能的(那么在哪里明确提到过?)或者只是编译器出了问题?
I can create an empty Java enum type, but when i want to add some methods : i get a "syntax error" (into Eclipse).
I found no official documentation about this, so my question is : is it just impossible (so where is it clearly mentioned?) or is it just the compiler which is wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,这是可能的。您只需添加
;
即可终止枚举常量的(空)列表:enum
s 的 JLS 语法定义(请注意,如果没有任何实例,您将只能调用
static< /代码>枚举的方法。)
相关:
Yes it's possible. You just need to add a
;
to terminate the (empty) list of enum constants:JLS Syntax Definition for
enum
s(Note that without any instances, you'll only be able to call the
static
methods of the Enum.)Related:
当然可以! “Ia!Ia!Cthulhu Fthagn!Ph'nglui mglw'nfah Cthulhu R'lyeh wgah'nagl fhtagn!!”
原始想法:“http://www.theserverside.com/news/thread.tss ?thread_id=50190"
摧毁现实的风险由您自己承担。
姆吴哈哈哈哈哈哈。
如果您确实需要一个枚举并且希望它具有实例方法,并且您已决定召唤古老的反射之神将这种可憎之物强加给世界,那么它有用。
这肯定会让其他开发人员感到困惑。
Of course you can! "Ia! Ia! Cthulhu Fthagn! Ph'nglui mglw'nfah Cthulhu R'lyeh wgah'nagl fhtagn!!"
Original idea: "http://www.theserverside.com/news/thread.tss?thread_id=50190"
Destroy reality at your own risk.
Mwu HA HA HA HA HA HA.
If you really need an Enum and you want it to have instance methods, and you have resolved to summon the elder gods of reflection to force this abomination upon the world, then it is useful.
This will definitely confuse the heck out of other developers later.
是的,是的:
您需要添加分号 (
;
) 来终止空枚举列表。编译:
虽然我不知道它有什么用。
Yes, it is:
You need to add a semicolon (
;
) to terminate the empty list of enums.This compiles:
Although I can't think what it would be useful for.
绝对,
稍后:
Absoulely,
Later: