標籤:
在 C/C++ 環境下,已經習慣使用枚舉型常量,但在 Android 下使用時發現枚舉與 C/C++ 下是完全不同的。
Android 下,枚舉其實是類。
使用感覺困難,主要是枚舉與 int 之間的轉換。如果枚舉的定義如下 weekday 所示,還可以通過 ordinal() 和 values()[] 方法進行轉換。
但不幸的是,我使用的是如下 weekday_2 所示的枚舉類型,我是沒有找到對應的轉換方法。
最後沒有辦法,將枚舉修改為一般的常量定義,如下:
1 public static final short MAX_BUFFER_SIZE = 2048;
1 public class Test { 2 public enum weekday 3 { 4 sun, 5 mou, 6 tue, 7 thu, 8 fri, 9 sat, 10 wed, 11 }; 12 13 public enum weekday_2 14 { 15 sun(0x1), 16 mou(0x2), 17 tue(0x3), 18 thu(0x4), 19 fri(0x5), 20 sat(0x6), 21 wed(0x7), 22 large(0x99); // 如果增加此值,通過 ordinal() 方法得到的值是多少? 23 24 private int iSet; 25 weekday_2(int iValue) { 26 this.iSet = iValue; 27 } 28 29 public int Get() { 30 return this.iSet; 31 } 32 }; 33 }
對於上面兩個枚舉定義 weekday 和 weekday_2:
1 int x = weekday.sun.ordinal(); 2 weekday sun = weekday.values()[x];
與
1 int x = weekday_2.sun.ordinal(); 2 weekday_2 sun = weekday_2.values()[x];
有什麼差別?除了 weekday_2 中的 large 成員外,其它成員的值是否相同?
1 void TestEnumValue() { 2 TstEnum.weekday day = TstEnum.weekday.sun; 3 TstEnum.weekday_2 day2 = TstEnum.weekday_2.sun; 4 TstEnum.weekday_2 day2_large = TstEnum.weekday_2.large; 5 6 int iDay = day.ordinal(); 7 int iDay2 = day2.ordinal(); 8 int iDay2Large = day2_large.ordinal(); 9 10 11 System.out.println("Enum Test" + "value of sun : " + Integer.toString(iDay) + "/" + Integer.toString(iDay2) 12 + ". Large: " + Integer.toString(iDay2Large)); 13 }
執行後輸出的結果是: Enum Testvalue of sun : 0/0. Large: 7
weekday_2 的成員 sun 並未輸出其真實的數值,而是輸出了下標。
對應之,weekday.values()[x]; 也是下標。若知道 weekday_2 的一個變數值是: 整型 0x99, 想轉成對應的枚舉。如果使用 weekday.values()[0x99] 則會直接越界。
(1) 執行:javac Test.java
編譯沒有出錯即可。
(2) 執行: javap Test
得到以下的輸出:
1 Compiled from "Test.java" 2 public class Test { 3 int x; 4 Test$weekday sun; 5 public Test(); 6 }
(3) 執行: javap Test$weekday
得到以下輸出:
1 Compiled from "Test.java" 2 public final class Test$weekday extends java.lang.Enum<Test$weekday> { 3 public static final Test$weekday sun; 4 public static final Test$weekday mou; 5 public static final Test$weekday tue; 6 public static final Test$weekday thu; 7 public static final Test$weekday fri; 8 public static final Test$weekday sat; 9 public static final Test$weekday wed; 10 public static Test$weekday[] values(); 11 public static Test$weekday valueOf(java.lang.String); 12 static {}; 13 }
(4) 執行: javap Test$weekday_2
得到以下輸出:
1 Compiled from "Test.java" 2 public final class Test$weekday_2 extends java.lang.Enum<Test$weekday_2> { 3 public static final Test$weekday_2 sun; 4 public static final Test$weekday_2 mou; 5 public static final Test$weekday_2 tue; 6 public static final Test$weekday_2 thu; 7 public static final Test$weekday_2 fri; 8 public static final Test$weekday_2 sat; 9 public static final Test$weekday_2 wed; 10 public static Test$weekday_2[] values(); 11 public static Test$weekday_2 valueOf(java.lang.String); 12 public int Get(); 13 static {}; 14 }
從以上反編譯出來的代碼可以得出以下結論:
(1) weekday 和 weekday_2 枚舉類是final class,即不能被繼承,而它本身是繼承於 Enum
(2) 枚舉值是 weekday 或 weekday_2 對象,而且是 static final 修飾的
Android 下枚舉型使用、及與 int 轉換的困惑