Java 枚举引用另一个枚举

发布于 2024-10-20 03:40:53 字数 250 浏览 2 评论 0原文

我希望能够定义一个枚举类,其中包含另一个枚举及其自己的值。例如,:

public enum A {
    A1, A2, A3;
}

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

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

谢谢,

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.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

撩人痒 2024-10-27 03:40:53

这样 BAB 的并集?如果没有B扩展A,这是不可能的,这是不支持的 在java中。继续的方法是创建一个包含 AB 组合值的 enum,例如,

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

Such that B is the union of A and B? That's not possible without B extending A, which is not supported in java. The way to proceed is to create an enum which contains the combined values of A and B, e.g.,

enum C {
    A1, A2, A3, B1, B2;
}
阳光下的泡沫是彩色的 2024-10-27 03:40:53

像这样的东西吗?

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

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;
    }
}
方觉久 2024-10-27 03:40:53

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

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

Joshua Bloch 在《Effective Java:第二版》中的第 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; }
    }

当涉及到枚举时,这几乎是最接近共享状态的了。正如 Johan Sjöberg 上面所说,将枚举简单地组合成另一个枚举可能是最简单的。

祝你好运!

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.

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. 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.

Best of luck!

揽清风入怀 2024-10-27 03:40:53

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

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

MyEnum e = B.B1;

该解决方案不适用于 switch 语句,也不适用于优化的枚举集合。

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

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

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.

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).

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