標籤:java .net
通過一段時間的項目實踐,發現java中的枚舉與.net中的枚舉有很大的差別,初期造成了我對java中的枚舉一些錯誤理解及部分有缺陷的應用,其實追其原因還是因為我會習慣性的認為java的枚舉在作用以及定義上與.net應該是差不多的,畢竟兩者都是進階語言,語言上也有很多相似之處。這就是老師傅常說的新手好教,老兵不好教的原因,新手腦子一片空白不會有任何幹擾,老兵總會以自己曾經的某些經驗與新知識做對比。
650) this.width=650;" src="http://images2015.cnblogs.com/blog/17071/201601/17071-20160127165050192-1054839953.jpg" width="270" height="241" style="border:0px;" />
習慣性觀點一:枚舉的定義應該與.net相同,比如在.net中我們可以這樣定義枚舉。
public enum EItemDataType { Real=1, Service=2 }
但java中並不能如此瀟洒的書寫枚舉,可能需要類似這樣寫:
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
public enum EItemDataType { Real(1),Service(2); private int value; private EItemDataType(int value) { this.value = value; } public int getValue() { return value; } public static EItemDataType valueOf(int value) { switch (value) { case 1: return EItemDataType.Real; case 2: return EItemDataType.Service; default: return null; } } }
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
發現.net要比java簡單的多,注意幾個方法:
var itemType=(EItemDataType)1;
var itemTypeValue=(int)EItemDataType.Real;
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
public enum EItemDataType { [Description("實物")] Real=1, [Description("服務")] Service=2 }
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
習慣性觀點二:因為.net的枚舉是個實值型別,所以我理所當然的會認為java的枚舉也是一個實值型別。之前對.net的理解就是將一些數值以更加可讀性的方式體現在程式中,比如訂單狀態,訂單類型等等,比如:
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
//枚舉值可讀性更強if(orderInfo.orderStatus.equals(EOrderStatus.Shipped)){ //do something}//一般不這樣寫,0可讀性不強if(orderInfo.orderStatus==0){ //do something}
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
枚舉類型的自說明:
650) this.width=650;" src="http://images2015.cnblogs.com/blog/17071/201601/17071-20160127165407910-1242841841.png" style="border:0px;" />
編譯之後所對應的位元組碼到底是什麼樣的:
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
public final class EItemDataType extends java.lang.Enum<EItemDataType> { public static final EItemDataType Real; public static final EItemDataType Service; static {}; Code: 0: new #1 // class EItemDataType 3: dup 4: ldc #15 // String Real 6: iconst_0 7: iconst_1 8: invokespecial #16 // Method "<init>":(Ljava/lang/String;II)V 11: putstatic #20 // Field Real:LEItemDataType; 14: new #1 // class EItemDataType 17: dup 18: ldc #22 // String Service 20: iconst_1 21: iconst_2 22: invokespecial #16 // Method "<init>":(Ljava/lang/String;II)V 25: putstatic #23 // Field Service:LEItemDataType; 28: iconst_2 29: anewarray #1 // class EItemDataType 32: dup 33: iconst_0 34: getstatic #20 // Field Real:LEItemDataType; 37: aastore 38: dup 39: iconst_1 40: getstatic #23 // Field Service:LEItemDataType; 43: aastore 44: putstatic #25 // Field ENUM$VALUES:[LEItemDataType; 47: return public int getValue(); Code: 0: aload_0 1: getfield #32 // Field value:I 4: ireturn public static EItemDataType valueOf(int); Code: 0: iload_0 1: tableswitch { // 1 to 2 1: 24 2: 28 default: 32 } 24: getstatic #20 // Field Real:LEItemDataType; 27: areturn 28: getstatic #23 // Field Service:LEItemDataType; 31: areturn 32: aconst_null 33: areturn public static EItemDataType[] values(); Code: 0: getstatic #25 // Field ENUM$VALUES:[LEItemDataType; 3: dup 4: astore_0 5: iconst_0 6: aload_0 7: arraylength 8: dup 9: istore_1 10: anewarray #1 // class EItemDataType 13: dup 14: astore_2 15: iconst_0 16: iload_1 17: invokestatic #42 // Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V 20: aload_2 21: areturn public static EItemDataType valueOf(java.lang.String); Code: 0: ldc #1 // class EItemDataType 2: aload_0 3: invokestatic #49 // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum; 6: checkcast #1 // class EItemDataType 9: areturn}
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
public final class EItemDataType extends java.lang.Enum<EItemDataType> {
public static final EItemDataType Real;
public static EItemDataType valueOf(java.lang.String);
這是個字串參數類型的方法,和我上面定義的valueOf(int value)很像,其目的都是根據一定的條件擷取枚舉值,只不過方式不同而已,前者是內建的根據枚舉值toString的結果來反向擷取枚舉值,與toString的對應,比如:EItemDataType.Real.toString()它等於“Real”,再調用EItemDataType.valueOf("Reail"),它等於EItemDataType.Real這個值。自訂的valueOf(int value)方式個人感覺並不太好,因為容易與內建的那個方法衝突,最好是改個名稱,比如value什麼。
650) this.width=650;" src="http://images2015.cnblogs.com/blog/17071/201601/17071-20160127164455988-965009830.jpg" style="border:0px;" />
最後我們再來看下枚舉所能實現的奇葩功能:單例(之前學習.net時寫的日記:老生常談:單件模式)。剛開始看到java的單例可以通過枚舉實現時,我都驚呆了,最大的反應是枚舉是個儲存值的怎麼和單例有關係?單例不是class的事嗎?其實通過上面的理解,枚舉就是個類,那麼再想想單例就不會有什麼疑問了,把它當成一個普通類不就好了,我們看一個簡單的計數的例子:按照上面位元組碼的結構,這個INSTANCE2會被定義成一個靜態變數,正是利用靜態變數唯一性的特性來實現了單例,而且是安全執行緒的。
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
public enum SafeSingleton implements Serializable { INSTANCE2; int count; public void addCount(int i) { this.count+=i; } public void printCount() { System.out.println(this.count); }}
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;" />
下面這段程式會輸出5050
for(int i=1;i<=100;i++){ SafeSingleton.INSTANCE2.addCount(i); } SafeSingleton.INSTANCE2.printCount();
java枚舉與.net中的枚舉區別