在 Java 中扩展枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6511453/
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
Extending a enum in Java
提问by user489041
I have an enum, let's call it A:
我有一个枚举,我们称之为 A:
public enum A
{
A,
B
}
I have a function that takes an enum A:
我有一个需要枚举 A 的函数:
public void functionA(A enumA)
{
//do something
}
How can I create another enum, possibly call B that I can pass to functionA? Something like this?
如何创建另一个枚举,可能调用 B 可以传递给functionA?像这样的东西?
public enum B
{
C
}
functionA(B.C);
I know that you can't extend an enum, but what other options do I have available? What is the best way to achieve this?
我知道您不能扩展枚举,但是我还有哪些其他选项可用?实现这一目标的最佳方法是什么?
采纳答案by Pa?lo Ebermann
You can't.
你不能。
Enum types are final by design.
枚举类型是最终设计的。
The reason is that each enum type should have only the elements declared in the enum(as we can use them in a switch statement, for example), and this is not possible if you allow extending the type.
原因是每个枚举类型应该只有在枚举中声明的元素(例如,我们可以在 switch 语句中使用它们),如果允许扩展类型,这是不可能的。
You might do something like this:
你可能会做这样的事情:
public interface MyInterface {
// add all methods needed here
}
public enum A implements MyInterface {
A, B;
// implement the methods of MyInterface
}
public enum B implements MyInterface {
C;
// implement the methods of MyInterface
}
Note that it is not possible to do a switch
with this interface, then. (Or in general have a switch
with an object which could come from more than one enum
).
请注意,无法switch
使用此接口进行操作。(或者通常有一个switch
可能来自多个的对象enum
)。
回答by Peter Lawrey
You can implement a common interface
您可以实现一个通用接口
interface I { }
enum A implements I {
A, B
}
enum B implements I {
C
}
public void functionA(I i) {
//do something
}
obj.functionA(A.A);
obj.functionA(B.C);
回答by ahans
You should add item C to your enum A. If it's something unrelated and adding it doesn't make sense, functionA() probably shouldn't be the one to handle it.
您应该将项目 C 添加到您的枚举 A 中。如果它不相关并且添加它没有意义,则 functionA() 可能不应该是处理它的人。
回答by Jesse Webb
You cannot make Enums extend other Enums, but you can declare an interface which all your Enums can implement. The interface will have to include any methods you expect to invoke on your Enum values. Here is an example:
您不能让 Enum 扩展其他 Enum,但您可以声明一个所有 Enum 都可以实现的接口。该接口必须包含您希望对 Enum 值调用的任何方法。下面是一个例子:
public class Question6511453 {
public static void main(String[] args) {
System.out.println(functionFoo(EnumA.FirstElem));
System.out.println(functionFoo(EnumA.SecondElem));
System.out.println(functionFoo(EnumB.ThirdElem));
}
private interface MyEnums {
int ordinal();
}
private enum EnumA implements MyEnums {
FirstElem,
SecondElem
}
private enum EnumB implements MyEnums {
ThirdElem
}
private static int functionFoo(MyEnums enumeration) {
return enumeration.ordinal();
}
}
The only problem with this solution is that it takes away your ability to use the Enum as you normally would like in switch statements, because the ordinals and values may not be unique anymore.
这个解决方案的唯一问题是它剥夺了您在 switch 语句中使用 Enum 的能力,因为序数和值可能不再是唯一的。
This should answer your question, but I doubt it will actually help you with your problem. :(
这应该可以回答您的问题,但我怀疑它是否真的能帮助您解决问题。:(