枚举(enum),是定义了一组排过序的单一实例的列表。
// 定义一个枚举 public enum Season { SPRING,SUMMER,AUTUMN,WINTER; }
反编译代码后可以发现,上述代码可以等价于
public final class Season extends Enum { public static final Season SPRING; public static final Season SUMMER; public static final Season AUTUMN; public static final Season WINTER; private static final Season ENUM$VALUES[]; static { SPRING = new Season("SPRING", 0); SUMMER = new Season("SUMMER", 1); AUTUMN = new Season("AUTUMN", 2); WINTER = new Season("WINTER", 3); ENUM$VALUES = (new Season[] { SPRING, SUMMER, AUTUMN, WINTER }); } }
使用方式
System.out.println(Season.AUTUMN);
输出结果为:AUTUMN