Java枚举继承 [英] Java enum inheritance
本文介绍了Java枚举继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
为枚举添加值
为什么Java中的枚举不能从其他枚举继承?为什么这样实现?
Why enums in Java cannot inherit from other enums? Why is this implemented this way?
推荐答案
因为向枚举添加元素
会有效地创建一个超
类,而不是子类。
Because adding elements to an enum would effectively create a super class, not a sub class.
考虑:
enum First {One, Two}
enum Second extends First {Three, Four}
First a = Second.Four; // clearly illegal
Second a = First.One; // should work
这与$ b的方式相反$ b适用于常规课程。我猜这个
可以用那种方式实现,但实现
比实际看起来更复杂,而b $ b肯定会让人感到困惑。
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.
这篇关于Java枚举继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文