如何将另一个枚举导入到我的枚举中
这是我的枚举
public enum MyEnum {Blue, Red};
还有另一个外部枚举,称为ExternalEnum,我没有也无法更改其源代码,但我可以使用该类,假设我知道其中有黄色和白色。
我想要做的是提取外部枚举中的所有元素,使它们成为我的枚举的一部分,即
public enum MyEnum {Blue, Red,ExternalEnum.Yellow,ExternalEnum.White};
有没有一种方法可以轻松地做到这一点,以便每次获得新版本的ExternalEnum 时,我不需要手动检查其所有元素?我不想使用扩展(子类),因为它们属于不同的包。
谢谢!
This is my Enum
public enum MyEnum {Blue, Red};
There is another external enum called ExternalEnum, I don't have and couldn't change its source code but I can use the class, say I know there are yellow and white in it.
What I want to do is to pull all the elements in the external enum, make them part of my enum, i.e.
public enum MyEnum {Blue, Red, ExternalEnum.Yellow, ExternalEnum.White};
Is there a way I can do this easily so each time I get a new version of ExternalEnum I don't need to manually go over all its elements? I don't want to use extend (subclass) as they belong to different package.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所说的是“动态枚举”。
这里是 StackOverflow 上一个问题的链接。
What you are talking about is a "Dynamic Enum".
Here is a link to a previous question here at StackOverflow.
您可以通过重新定义枚举常量来做到这一点,如下所示:
但是,您必须确保枚举常量的整数值不重叠。最简单的方法是首先声明外部常量。不存在自动导入枚举之类的东西。您无法扩展枚举。
You can do it by redefining the enum constants like this:
However, you must ensure that the integer values of the enum constants do not overlap. The easiest way of doing it, is to declare the external constants first. There is no such thing like an automatic import into enums. You cannot extend enums.
由于您没有指定语言,因此很难为您提供帮助,但我怀疑您想要做的事情在解释语言中可能比编译语言更容易。
Since you don't specify a lnguage it is very difficult to help you, but I suspect that what you want to do might be easier in interpreted languages than compiled.