Java枚举继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1884433/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:33:08  来源:igfitidea点击:

Java enum inheritance

java

提问by Szymon Lipiński

Possible Duplicate:
add values to enum

可能的重复:
向枚举添加值

Why enums in Java cannot inherit from other enums? Why is this implemented this way?

为什么 Java 中的枚举不能从其他枚举继承?为什么以这种方式实现?

采纳答案by TofuBeer

Example stolen from here

从这里偷来的例子

Because adding elements to an enum would effectively create a super class, not a sub class.

Consider:

因为将元素添加到枚举将有效地创建一个超类,而不是一个子类。

考虑:

 enum First {One, Two}   
 enum Second extends First {Three, Four}   

 First a = Second.Four;   // clearly illegal 
 Second a = First.One;  // should work

This is the reverse of the way it works with regular classes. I guess it could be implemented that way but it would be more complicated to implement than it would seems, and it would certainly confuse people.

这与常规类的工作方式相反。我想它可以通过这种方式实施,但实施起来会比看起来更复杂,而且肯定会让人们感到困惑。