可以用方法创建一个空的Java枚举类型吗? [英] Is it possible to create an empty Java enum type with methods?
本文介绍了可以用方法创建一个空的Java枚举类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我没有找到关于这个的官方文档,所以我的问题是:这是不可能的(所以在哪里清楚地提到?)还是只是编译器是错误的?
解决方案
是的,这是可能的。您只需要添加一个;
来终止枚举常量的(空)列表:
code>枚举TestEnum {
; //没有这个没有编译。
public void hello(){
System.out.println(Hello World);
}
}
(请注意,没有任何实例,您只能调用枚举的 static
方法。 )
相关:
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 ?
解决方案
Yes it's possible. You just need to add a ;
to terminate the (empty) list of enum constants:
enum TestEnum {
; // doesn't compile without this.
public void hello() {
System.out.println("Hello World");
}
}
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:
这篇关于可以用方法创建一个空的Java枚举类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文