Android 下枚舉型使用、及與 int 轉換的困惑

來源:互聯網
上載者:User

標籤:

在 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 轉換的困惑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.