我将如何列出枚举?

时间:2019-04-10 21:31:54

标签: java enums

我想创建一个包含几个枚举的列表。 例如,包含“天和天气”的列表。

public enum Days {
SUNDAY,MONDAY,TUESDAY,WENESDAY,THURSDAY,FRIDAY,SATURDAY;
}

public enum Weather {
CLOUDY, CLEARSKIES, SUNNY, RAIN, SNOW;
}

2 个答案:

答案 0 :(得分:0)

您可以考虑一个接口,并使它们都实现该接口。然后,您将存储实现该接口的对象的列表。

func, allowed = allowed, ['*']

样品用量:

interface Parent {

}

public enum Days implements Parent {
    SUNDAY,MONDAY,TUESDAY,WENESDAY,THURSDAY,FRIDAY,SATURDAY;
}

public enum Weather implements Parent {
    CLOUDY, CLEARSKIES, SUNNY, RAIN, SNOW;
}

答案 1 :(得分:0)

您可以将它们包裹起来

class EnumWrapper {
    final Enum<?> val;
    final boolean isWeather;

    EnumWrapper(Days d) {
        val = d;
    }

    EnumWrapper(Weather w) {
        val = w;
        isWeather = true;
    }

    Days getDay() {
        if(!isWeather) return (Days) val;
        return null;
    }

    Weather getWeather() {
        if(isWeather) return (Weather) val;
        return null;
    }
}