什么是枚舉?
枚舉是JDK5引入的新特性。在某些情況下,一個類的對象是固定的,就可以定義為枚舉。在實際使用中,枚舉類型也可以作為一種規范,保障程序參數安全。枚舉有以下特點:
- Java中枚舉和類、接口的級別相同。
- 枚舉和類一樣,都有自己的屬性、方法、構造方法,不同點是:枚舉的構造方法只能是private修飾,也就無法從外部構造對象。構造方法只在構造枚舉值時調用。
-
使用enum關鍵字聲明一個枚舉類型時,就默認繼承自Java中的
java.lang.Enum
類,并實現了java.lang.Seriablizable
和java.lang.Comparable
兩個接口。 -
所有的枚舉值都是
public static final
的,且非抽象的枚舉類不能再派生子類。 - 枚舉類的所有實例(枚舉值)必須在枚舉類的第一行顯式地列出,否則這個枚舉類將永遠不能產生實例。
- 判斷枚舉是否相同時,使用 == 和 equals 是一樣的。
下面是 java.lang.Enum
類中的 equals()
:
1
2
3
4
|
// 這里是final修飾的,不允許子類重寫 public final boolean equals(Object other) { return this ==other; } |
枚舉的常用方法
int compareTo(E o)
比較此枚舉與指定對象的順序。在該對象小于、等于或大于指定對象時,分別返回負整數、零或正整數。 枚舉常量只能與相同枚舉類型的其他枚舉常量進行比較。
1
2
3
4
5
6
7
8
9
|
// Enum 中的源碼 public final int compareTo(E o) { Enum other = (Enum)o; Enum self = this ; if (self.getClass() != other.getClass() && // optimization self.getDeclaringClass() != other.getDeclaringClass()) throw new ClassCastException(); return self.ordinal - other.ordinal; } |
String name()
返回此枚舉實例的名稱。
static values()
返回一個包含全部枚舉值的數組,可以用來遍歷所有枚舉值。
String toString()
返回此枚舉實例的名稱,即枚舉值。與 name()
一樣。
1
2
3
4
5
6
7
|
// Enum 中 name() 和 toString() public String toString() { return name; } public final String name() { return name; } |
int ordinal()
返回枚舉值在枚舉類中的索引值(從0開始),即枚舉值在枚舉聲明中的順序,這個順序根據枚舉值聲明的順序而定。
<T extends Enum<T>> valueOf()
返回帶指定名稱的指定枚舉類型的枚舉常量,名稱必須與在此類型中聲明枚舉常量所用的標識符完全匹配(不允許使用額外的空白字符)。這個方法與toString相對應,因此重寫 toString()
方法,一定要重寫 valueOf()
方法(我們可以重寫 toString()
方法,但不能自己重寫 valueOf()
方法,當我們重寫 toString()
方法時,valueOf()
方法會自動重寫,不用我們理會。)
枚舉的應用
枚舉是一種特殊的類型,其用法和普通的類使用非常相似。
代替一組常量
1
2
3
|
public enum Color { RED, GREEN, BLANK, YELLOW } |
switch 語句中使用
1
2
3
4
5
6
7
8
9
10
11
12
|
// JDK1.6 中switch加入了對枚舉的支持 enum Signal { GREEN, YELLOW, RED } ... switch (color) { case RED: color = Signal.GREEN; break ; } ... |
向枚舉中添加方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public enum Color { RED( "紅色" ), GREEN( "綠色" ), BLANK( "白色" ), YELLO( "黃色" ); // 成員變量 private String name; // 構造方法 private Color(String name) { this .name = name; } // get set 方法 public String getName() { return name; } public void setName(String name) { this .name = name; } } |
實現接口
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public interface Behaviour { void print(); } public enum Color implements Behaviour{ RED( "紅色" , 1 ), GREEN( "綠色" , 2 ), BLANK( "白色" , 3 ), YELLO( "黃色" , 4 ); //接口方法 @Override public void print() { System.out.println( this .index+ ":" + this .name); } } |
包含抽象方法的枚舉類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
public enum Operation { // 用于執行加法運算 PLUS { // 花括號部分其實是一個匿名內部子類 @Override public double calculate( double x, double y) { return x + y; } }, // 用于執行減法運算 MINUS { // 花括號部分其實是一個匿名內部子類 @Override public double calculate( double x, double y) { // TODO Auto-generated method stub return x - y; } }, // 用于執行乘法運算 TIMES { // 花括號部分其實是一個匿名內部子類 @Override public double calculate( double x, double y) { return x * y; } }, // 用于執行除法運算 DIVIDE { // 花括號部分其實是一個匿名內部子類 @Override public double calculate( double x, double y) { return x / y; } }; //為該枚舉類定義一個抽象方法,枚舉類中所有的枚舉值都必須實現這個方法 public abstract double calculate( double x, double y); } |
使用枚舉實現單例(單例的最佳實踐)
好處:
1.利用的枚舉的特性實現單例
2.由JVM保證線程安全
3.序列化和反射攻擊已經被枚舉解決
1
2
3
4
5
6
7
|
public enum Singleton { INSTANCE; public Singleton getInstance(){ // 增加這個方法是讓別人明白怎么使用,因為這種實現方式還比較少見。 return INSTANCE; } } |
其他關于枚舉的使用
EnumSet
range(E from, E to)
從枚舉值中獲取一段范圍的 Set。
1
2
3
|
for (WeekDayEnum day : EnumSet.range(WeekDayEnum.Mon, WeekDayEnum.Fri)) { System.out.println(day); } |
of(E first, E... rest)
創建一個最初包含指定元素的枚舉 Set。
noneOf(Class<E> elementType)
創建一個具有指定元素類型的空枚舉 Set。
EnumMap
EnumMap(Class<K> keyType)
創建一個具有指定鍵類型的空枚舉Map。
1
2
3
|
Map<Weather, String> enumMap = new EnumMap<Weather, String>(Weather. class ); enumMap.put(Weather.Sunny, "晴天" ); enumMap.put(Weather.Rainy, "雨天" ); |
Android中的枚舉
Enum 需要占用較大的內存,如果對內存敏感,請盡量少使用 Enum,換成靜態常量。
但是如果不使用枚舉,會出現一些安全隱患,所以官方推出了兩個注解,可以在編譯時期進行類型檢查,以此替代枚舉。這兩個注解分別是:@IntDef 和 @StringDef。位于compile 'com.android.support:support-annotations:+' 。
使用示例
@StringDef的使用與 @IntDef一致,這里以 @IntDef為例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public interface QRCodeType { int WECHAT = 0 ; int ALIPAY = 1 ; @IntDef ({WECHAT , ALIPAY }) @Retention (RetentionPolicy.SOURCE) @Target ({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) @interface Checker { } } public class QRCode { @QRCodeType .Checker // 定義在屬性 private int type; public void setType( @QRCodeType .Checker int type) { // 定義在參數 this .type= type; } @QRCodeType .Checker // 定義在方法(也就是檢查返回值的類型) public int getType() { return type; } } |
使用建議
開發中使用范圍最廣的就是利用枚舉代替一組靜態常量,這種情況可以使用以上注解方式替代。
當枚舉還含有其它功能時(如:包含其它定義的方法),則不能替換。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
原文鏈接:http://www.jianshu.com/p/6b7f715d06f6