- 使用enum关键字定义
- 提供枚举类的对象,多个对象用逗号分开,结尾用分号结束
- 提供构造方法,给属性赋值
- 使用public statiac final来修饰是属性
- 提供get方法
- 提供toString方法
public enum Season {
SPRING("春天", "春暖花开"),
SUMMER("夏天", "夏日炎炎"),
AUTUMN("秋天", "秋高气爽"),
WINTER("冬天", "冰天雪地");
private final String seasonName;
private final String des;
Season(String seasonName, String des) {
this.seasonName = seasonName;
this.des = des;
}
public String getSeasonName() {
return seasonName;
}
public String getDes() {
return des;
}
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", des='" + des + '\'' +
'}';
}
}
Season[] values = Season.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
结果
(Thread)源码示例:
public static enum State {
NEW,
RUNNABLE,
BLOCKED,
WAITING,
TIMED_WAITING,
TERMINATED;
private State() {
}
}
Thread.State[] states = Thread.State.values();
for (int i = 0; i < states.length; i++) {
System.out.println(states[i]);
}