Java枚举引用另一个枚举 [英] Java Enum referencing another enum

查看:736
本文介绍了Java枚举引用另一个枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够定义一个Enum类,该类将包含另一个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)
}

使得枚举B中包含的任何值都必须自己定义(例如B1,​​B2)或枚举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 A B 的并集吗?如果没有 B 扩展 A ,这是不可能的,这是在Java中不受支持。进行方法是创建一个枚举,其中包含 A B的组合值,例如

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

这篇关于Java枚举引用另一个枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆