假设,我在java中有以下enum类,我将如何使用不同的方式访问它?
public enum DayOfWeek {
SUNDAY(0),
MONDAY(1),
TUESDAY(2),
WEDNESDAY(3),
THURSDAY(4),
FRIDAY(5),
SATURDAY(6);
Integer label;
DayOfWeek(Integer label) {
this.label = label;
}
public Integer getValue() {
return label;
}
}
返回Enum类型的对象:
DayOfWeek myType = DayOfWeek.MONDAY;
返回Integer类型的标签:
Integer myInt = DayOfWeek.TUESDAY.getValue(); // 1
返回指定名称的指定Enum类型的Enum常量:
DayOfWeek myType = DayOfWeek.valueOf("THURSDAY");
<扔strong>IllegalArgumentException 如果指定的枚举类型没有具有指定名称的常量或NullPointerException如果指定的类对象不代表枚举类型(例如null)扔strong>
返回Enum对象的默认字符串表示形式:
String myEnumName = DayOfWeek.WEDNESDAY.toString(); // "WEDNESDAY"
返回所有Enum类型作为数组:
DayOfWeek[] values = DayOfWeek.values();