简体   繁体   English

是否可以使用方法创建一个空的Java枚举类型?

[英]Is it possible to create an empty Java enum type with methods?

I can create an empty Java enum type, but when i want to add some methods : i get a "syntax error" (into Eclipse). 我可以创建一个空的Java枚举类型,但是当我想添加一些方法时:我得到一个“语法错误”(进入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 enum的JLS语法定义

(Note that without any instances, you'll only be able to call the static methods of the Enum.) (注意,没有任何实例,您只能调用Enum的static方法。)

Related: 有关:

Of course you can! 当然可以! "Ia! Ia! Cthulhu Fthagn! Ph'nglui mglw'nfah Cthulhu R'lyeh wgah'nagl fhtagn!!" “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 " 最初的想法:“ http://www.theserverside.com/news/thread.tss?thread_id=50190

Destroy reality at your own risk. 以自己的风险摧毁现实。

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

enum ThingsThatShouldNotBe {
;
public void HelloWorld() {
    System.out.println(this.name() + " Says Hello!");
}
public static void main(String[] args) throws Exception {
    Constructor<?> con = ThingsThatShouldNotBe.class.getDeclaredConstructors()[0];
    Method[] methods = con.getClass().getDeclaredMethods();
    for (Method m : methods) {
        if (m.getName().equals("acquireConstructorAccessor")) {
            m.setAccessible(true);
            m.invoke(con, new Object[0]);
        }
    }
    Field[] fields = con.getClass().getDeclaredFields();
    Object ca = null;
    for (Field f : fields) {
        if (f.getName().equals("constructorAccessor")) {
            f.setAccessible(true);
            ca = f.get(con);
        }
    }
    Method m = ca.getClass().getMethod("newInstance",
        new Class[] { Object[].class });
    m.setAccessible(true);
    ThingsThatShouldNotBe v = (ThingsThatShouldNotBe) m.invoke(ca, new Object[] { new Object[] { "Cthulhu",
            Integer.MAX_VALUE } });
    System.out.println(v.getClass() + ":" + v.name() + ":" + v.ordinal());
    System.out.println("Say hello Cthulhu!");
    v.HelloWorld();
}
}

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 . 如果你真的需要一个Enum并且你想让它拥有实例方法,并且你已经决定召唤年长的反思之神来强迫这个可憎的世界,那么它是有用的

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: 这编译:

public enum MyEnum {
    ;
    public void method() {

    }
}

Although I can't think what it would be useful for. 虽然我想不出它会有什么用处。

Absoulely, Absoulely,

/**
 * @author The Elite Gentleman
 * @since 06 September 2011
 *
 */
public enum ExampleEnum {
    WHAT_ENUM
    ;

    public void doOperation() {

    }
}

Later: 后来:

ExampleEnum exEnum = ExampleEnum.WHAT_ENUM;
exEnum.doOperation();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM