Can enums be subclassed to add new elements?

前端 未结 15 2010
臣服心动
臣服心动 2020-11-22 12:02

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}*/

Is this po

15条回答
  •  庸人自扰
    2020-11-22 12:18

    The recommended solution to this is the extensible enum pattern.

    This involves creating an interface and using that where you currently use the enum. Then make the enum implement the interface. You can add more constants by making that new enum also extend the interface.

提交回复
热议问题