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

发布于 2024-12-02 21:52:31 字数 118 浏览 3 评论 0原文

我可以创建一个空的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

澉约 2024-12-09 21:52:31

是的,这是可能的。您只需添加 ; 即可终止枚举常量的(空)列表:

enum TestEnum {
    ;                         // doesn't compile without this.
    public void hello() {
        System.out.println("Hello World");
    }
}

enums 的 JLS 语法定义

(请注意,如果没有任何实例,您将只能调用 static< /代码>枚举的方法。)

相关:

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 enums

(Note that without any instances, you'll only be able to call the static methods of the Enum.)

Related:

悲欢浪云 2024-12-09 21:52:31

当然可以! “Ia!Ia!Cthulhu Fthagn!Ph'nglui mglw'nfah Cthulhu R'lyeh wgah'nagl fhtagn!!”

原始想法:“http://www.theserverside.com/news/thread.tss ?thread_id=50190"

摧毁现实的风险由您自己承担。

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();
}
}

姆吴哈哈哈哈哈哈。

如果您确实需要一个枚举并且希望它具有实例方法,并且您已决定召唤古老的反射之神将这种可憎之物强加给世界,那么它有用

这肯定会让其他开发人员感到困惑。

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.

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.

This will definitely confuse the heck out of other developers later.

月寒剑心 2024-12-09 21:52:31

是的,是的:

您需要添加分号 (;) 来终止空枚举列表。
编译:

public enum MyEnum {
    ;
    public void method() {

    }
}

虽然我不知道它有什么用。

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.

望喜 2024-12-09 21:52:31

绝对,

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

    public void doOperation() {

    }
}

稍后:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文