Java enum inheritance [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

Possible Duplicate:
add values to enum

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

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!