枚舉應(yīng)該是一個有限集合組成的一個對象
一個簡單的例子:
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
枚舉最簡單的就是當(dāng)常量用起惕,但是比常量安全呈枉,最常用的就是在switch里面哲泊。
其實枚舉可以當(dāng)個普通的類使用,可以有變量舷夺,方法等苦酱。
當(dāng)普通類使用的時候,其中有幾個注意點:
- enum不可以有子類给猾,因為enum是final的疫萤,
- enum不是繼承其它類,因為它默認(rèn)已經(jīng)繼承了Enum敢伸,但是可以實現(xiàn)Interface
- 構(gòu)造方法是private或者是默認(rèn)扯饶,不能是public的或者protected的
下面一個用作普通類的例子:
package tenum;
interface Next<T> {
T next();
}
public enum Week implements Next<Week> {
SUNDAY("Today is Sunday."), MONDAY("Today is Monday"), TUESDAY("Today is Tuesday"), WEDNESDAY(
"Today is wednesday"), THURSDAY(
"Today is Thursday"), FRIDAY("Today is Firday"), SATURDAY("Today is Saturday");
private String description;
Week(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
@Override
public Week next() {
switch (this) {
case SUNDAY:
return MONDAY;
case MONDAY:
return TUESDAY;
case TUESDAY:
return WEDNESDAY;
case WEDNESDAY:
return THURSDAY;
case THURSDAY:
return FRIDAY;
case FRIDAY:
return SATURDAY;
case SATURDAY:
return SUNDAY;
}
return null;
}
public static void main(String[] args) {
for (Week w : values()) {
System.out.println(w.getDescription());
}
System.out.println("------------------------------");
for (Week w : Week.class.getEnumConstants()) {
System.out.println(w.getDescription());
}
System.out.println("------------------------------");
//星期一之后2天是周幾
System.out.println(Week.MONDAY.next().next().getDescription());
}
}
輸出結(jié)果:
Today is Sunday.
Today is Monday
Today is Tuesday
Today is wednesday
Today is Thursday
Today is Firday
Today is Saturday
------------------------------
Today is Sunday.
Today is Monday
Today is Tuesday
Today is wednesday
Today is Thursday
Today is Firday
Today is Saturday
------------------------------
Today is wednesday
上面兩種來獲取所有元素,一種是調(diào)用values()
方法池颈,一種是通過class 對象的getEnumConstants()
方法尾序。
有趣的是values()
方法,enum 是繼承Eunm.java
躯砰,但是Enum.java
里面并沒有values()
方法每币。怎么來的呢?其實是編譯器為我們自動生成的琢歇。如果你自己在定義這個方法兰怠,編譯器就會報錯“The enum Week already defines the method values() implicitly”。
讓我們來看看編譯器生成的class 文件李茫。
Warning: Binary file Week contains tenum.Week
Compiled from "Week.java"
public final class tenum.Week extends java.lang.Enum<tenum.Week> implements tenum.Next<tenum.Week> {
public static final tenum.Week SUNDAY;
public static final tenum.Week MONDAY;
public static final tenum.Week TUESDAY;
public static final tenum.Week WEDNESDAY;
public static final tenum.Week THURSDAY;
public static final tenum.Week FRIDAY;
public static final tenum.Week SATURDAY;
static {};
public java.lang.String getDescription();
public tenum.Week next();
public static void main(java.lang.String[]);
public static tenum.Week[] values();
public static tenum.Week valueOf(java.lang.String);
public java.lang.Object next();
static int[] $SWITCH_TABLE$tenum$Week();
}
從上面可以看到幾點揭保,1)class 是final的,2)自動生成values()方法魄宏,3)繼承了Enum類
參考:
https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
https://www.ibm.com/developerworks/cn/java/j-lo-enum/index.html