可以枚举枚举以添加新元素吗? [英] Can enums be subclassed to add new elements?
本文介绍了可以枚举枚举以添加新元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取现有的枚举并添加更多元素,如下所示:
I want to take an existing enum and add more elements to it as follows:
enum A {a,b,c}
enum B extends A {d}
/*B is {a,b,c,d}*/
这在Java中是否可行?
Is this possible in Java?
推荐答案
不,你不能用Java做到这一点。除此之外, d
可能会是 A
的一个实例(假设扩展的正常概念) ,但只知道 A
的用户不会知道它 - 这会使枚举点成为一组众所周知的值。
No, you can't do this in Java. Aside from anything else, d
would then presumably be an instance of A
(given the normal idea of "extends"), but users who only knew about A
wouldn't know about it - which defeats the point of an enum being a well-known set of values.
如果您可以告诉我们更多关于您如何使用的信息,我们可以建议其他解决方案。
If you could tell us more about how you want to use this, we could potentially suggest alternative solutions.
这篇关于可以枚举枚举以添加新元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文