我可以从两个枚举创建一个枚举(在switch语句中使用的常量)吗?
就像是...

public class Test{

    public enum WorkDay {Monday, Tuesday, Wednesday, Thursday,Friday};
    public enum WeekEndDay {Sunday, Saturday};

    // it doesn't work ):
    public enum Day{WorkDay.values(),WeekEndDay.values()};

    public static void Action(Day d) {

        switch(d){
            case Monday:
                ...;
                break;
            case Tuesday:
                ...;
                break;
            case ...:
                ...;
                break;
            case Saturday:
                ...;
                break;
        }
    }
}


任何想法???我真的很想使用switch并将枚举分开
谢谢!!!

最佳答案

不。您可以创建一个Day枚举并显式列出每个值,同时也为每种类型提供构造函数...但是我个人会使用:

public enum Day {
    MONDAY(true),
    TUESDAY(true),
    WEDNESDAY(true),
    THURSDAY(true),
    FRIDAY(true),
    SATURDAY(false),
    SUNDAY(false);

    private final boolean workDay;

    private Day(boolean workDay) {
        this.workDay = workDay;
    }

    public boolean isWorkDay() {
        return workDay;
    }
}


然后,您还可以在工作日发布EnumSet<Day>,在非工作日发布另一个。

10-08 17:03