Android 下枚举型使用、及与 int 转换的困惑

在 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 修饰的

转载于:https://www.cnblogs.com/91program/p/5192293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值