本论坛将全面搬家到:http://www.cnblogs.com/91program,请大家以后来这里看看。
在 C/C++ 环境下,已经习惯使用枚举型常量,但在 Android 下使用时发现枚举与 C/C++ 下是完全不同的。
Android 下,枚举其实是类。public static final short MAX_BUFFER_SIZE = 2048;
public class Test {
public enum weekday
{
sun,
mou,
tue,
thu,
fri,
sat,
wed,
};
public enum weekday_2
{
sun(0x1),
mou(0x2),
tue(0x3),
thu(0x4),
fri(0x5),
sat(0x6),
wed(0x7),
large(0x99); // 如果增加此值,通过 ordinal() 方法得到的值是多少?
private int iSet;
weekday_2(int iValue) {
this.iSet = iValue;
}
public int Get() {
return this.iSet;
}
};
}
int x = weekday.sun.ordinal();
weekday sun = weekday.values()[x];
与
int x = weekday_2.sun.ordinal();
weekday_2 sun = weekday_2.values()[x];
有什么差别?除了 weekday_2 中的 large 成员外,其它成员的值是否相同?
void TestEnumValue() {
TstEnum.weekday day = TstEnum.weekday.sun;
TstEnum.weekday_2 day2 = TstEnum.weekday_2.sun;
TstEnum.weekday_2 day2_large = TstEnum.weekday_2.large;
int iDay = day.ordinal();
int iDay2 = day2.ordinal();
int iDay2Large = day2_large.ordinal();
System.out.println("Enum Test" + "value of sun : " + Integer.toString(iDay) + "/" + Integer.toString(iDay2)
+ ". Large: " + Integer.toString(iDay2Large));
}
Compiled from "Test.java"
public class Test {
int x;
Test$weekday sun;
public Test();
}
Compiled from "Test.java"
public final class Test$weekday extends java.lang.Enum {
public static final Test$weekday sun;
public static final Test$weekday mou;
public static final Test$weekday tue;
public static final Test$weekday thu;
public static final Test$weekday fri;
public static final Test$weekday sat;
public static final Test$weekday wed;
public static Test$weekday[] values();
public static Test$weekday valueOf(java.lang.String);
static {};
}
Compiled from "Test.java"
public final class Test$weekday_2 extends java.lang.Enum {
public static final Test$weekday_2 sun;
public static final Test$weekday_2 mou;
public static final Test$weekday_2 tue;
public static final Test$weekday_2 thu;
public static final Test$weekday_2 fri;
public static final Test$weekday_2 sat;
public static final Test$weekday_2 wed;
public static Test$weekday_2[] values();
public static Test$weekday_2 valueOf(java.lang.String);
public int Get();
static {};
}