Java 枚举引用另一个枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5172178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:48:52  来源:igfitidea点击:

Java Enum referencing another enum

javaenums

提问by MikeMY

I would like to be able to define an Enum class that would include another Enum as well as its own values. For example, :

我希望能够定义一个 Enum 类,该类将包含另一个 Enum 以及它自己的值。例如, :

public enum A {
    A1, A2, A3;
}

public enum B{
    B1, B2, ...(somehow define references to all values in enum A)
}

such that any values contained in enum B must be either defined initself (such as B1, B2) or any values of enum A.

使得枚举 B 中包含的任何值必须是自身定义的(例如 B1、B2)或枚举 A 的任何值。

Thanks,

谢谢,

回答by Johan Sj?berg

Such that Bis the union of Aand B? That's not possible without Bextending A, which is not supportedin java. The way to proceed is to create an enumwhich contains the combined values of Aand B, e.g.,

B就是Aand 的并集B?如果没有B扩展A,这是不可能的,Java不支持扩展。继续进行该方法是创建一个enum包含的组合值AB,例如

enum C {
    A1, A2, A3, B1, B2;
}

回答by ILMTitan

Something like this?

像这样的东西?

enum A {
    A1, A2;
}

enum B {
    B1(null),
    B2(null),
    A1(A.A1),
    A2(A.A2);

    private final A aRef;
    private final B(A aRef) {
        this.aRef = aRef;
    }
}

回答by jhhurwitz

Typically, since we are working in java, and usually proscribe to OO design, you could get this functionality through extension. However, enums extend java.lang.Enum implicitly and therefore cannot extend another class.

通常,由于我们在 Java 中工作,并且通常禁止 OO 设计,因此您可以通过扩展来获得此功能。但是,枚举隐式扩展 java.lang.Enum,因此不能扩展另一个类。

This means extensibility for enums most be derived from a different source.

这意味着枚举的可扩展性大多来自不同的来源。

Joshua Bloch addesses this in Effective Java: Second Editionin item 34, when he presents a shared interface pattern for enums. Here is his example of the pattern:

Joshua Bloch 在Effective Java: Second Edition的第 34 条中补充了这一点,当时他提出了枚举的共享接口模式。这是他的模式示例:

   public interface Operation {
        double apply(double x, double y);
    }

    public enum BasicOperation implements Operation {
        PLUS("+") {
            public double apply(double x, double y) { return x + y; }
        },
        MINUS("-") {
            public double apply(double x, double y) { return x - y; }
        },
        TIMES("*") {
            public double apply(double x, double y) { return x * y; }
        },
        DIVIDE("/") {
            public double apply(double x, double y) { return x / y; }
    }

This is about as close as you can get to shared state when it comes to enums. As Johan Sj?berg said above, it might just be easiest to simply combine the enums into another enum.

当涉及到枚举时,这与您可以达到的共享状态差不多。正如上面 Johan Sj?berg 所说,将枚举简单地组合到另一个枚举中可能是最简单的。

Best of luck!

祝你好运!

回答by josefx

The closest you can get is to have A and B implement some shared interface.

最接近的是让 A 和 B 实现一些共享接口。

enum A implements MyEnum{A1,A2}
enum B implements MyEnum{B1,B2}

MyEnum e = B.B1;

This solution wont work with the switch statement nor with the optimised enum collections.

此解决方案不适用于 switch 语句和优化的枚举集合。

Your use-case is not supported by the java enum implementation, it was either to complex to implement (compared to its usefulness) or it didn't come up (none of the few languages that I know support this directly).

java enum 实现不支持您的用例,它要么实现起来很复杂(与其有用性相比),要么没有出现(我知道的少数几种语言都没有直接支持这一点)。