简体   繁体   English

Java Enum引用另一个枚举

[英]Java Enum referencing another enum

I would like to be able to define an Enum class that would include another Enum as well as its own values. 我希望能够定义一个包含另一个枚举的枚举类以及它自己的值。 For example, : 例如, :

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中包含的任何值必须定义为initself(例如B1,​​B2)或枚举A的任何值。

Thanks, 谢谢,

Such that B is the union of A and B ? 那么BAB的联合? That's not possible without B extending A , which is not supported in java. 如果没有B扩展A ,这是不可能 A ,这在java中是不受支持的。 The way to proceed is to create an enum which contains the combined values of A and B , eg, 继续的方法是创建一个包含AB的组合值的enum ,例如,

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

Typically, since we are working in java, and usually proscribe to OO design, you could get this functionality through extension. 通常,由于我们在java中工作,并且通常禁止使用OO设计,因此您可以通过扩展来获得此功能。 However, enums extend java.lang.Enum implicitly and therefore cannot extend another class. 但是,枚举会隐式扩展java.lang.Enum,因此无法扩展另一个类。

This means extensibility for enums most be derived from a different source. 这意味着枚举的可扩展性大部分来自不同的来源。

Joshua Bloch addesses this in Effective Java: Second Edition in item 34, when he presents a shared interface pattern for enums. Joshua Bloch在 34项中的Effective Java:Second Edition中对此进行了广告,当时他为枚举提供了一个共享接口模式。 Here is his example of the pattern: 以下是他的模式示例:

   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. 正如JohanSjöberg上面所说的,简单地将枚举结合到另一个枚举中可能是最简单的。

Best of luck! 祝你好运!

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

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实现不支持你的用例,要么实现复杂(与其实用性相比),要么它没有出现(我所知道的几种语言都没有直接支持)。

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

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