Can enums be subclassed to add new elements?

前端 未结 15 1850
臣服心动
臣服心动 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:30

    Based on @Tom Hawtin - tackline answer we add switch support,

    interface Day {
        ...
      T valueOf();
    }
    
    public enum Weekday implements Day {
        MON, TUE, WED, THU, FRI;
       Weekday valueOf(){
         return valueOf(name());
       }
    }
    
    public enum WeekendDay implements Day {
        SAT, SUN;
       WeekendDay valueOf(){
         return valueOf(name());
       }
    }
    
    Day wds = Weekday.MON;
    Day wends = WeekendDay.SUN;
    
    switch(wds.valueOf()){
        case MON:
        case TUE:
        case WED:
        case THU:
        case FRI:
    }
    
    switch(wends.valueOf()){
        case SAT:
        case SUN:
    }
    

提交回复
热议问题