Java枚舉基本用法

枚舉(enum),是定義了一組排過序的單一實例的列表。spa

// 定義一個枚舉
public enum Season { SPRING,SUMMER,AUTUMN,WINTER; }

反編譯代碼後能夠發現,上述代碼能夠等價於code

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 }); } }

使用方式blog

System.out.println(Season.AUTUMN);

輸出結果爲:AUTUMN編譯

相關文章
相關標籤/搜索