您的位置:首页 > 移动开发 > Android开发

Android中枚举enum的使用

2016-02-02 15:05 411 查看

一、枚举说明和注意:

1)枚举类也是一种特殊形式的Java类。

2)枚举类中声明的每一个枚举值代表枚举类的一个实例对象。所以每个实例对象的实例化过程都会调用一次构造函数

3)与java中的普通类一样,在声明枚举类时,也可以声明属性、方法和构造函数,但枚举类的构造函数必须为私有的(个人理解:因为构造函数仅供枚举内的实例化过程调用)。

4)枚举类也可以实现接口、或继承抽象类。

5)如果枚举内有构造函数或方法等,那么枚举的实例最后必须使用;号

二、枚举常用方法介绍:

1)int compareTo(E o)

比较此枚举与指定对象的顺序。

2)Class<E> getDeclaringClass()

返回与此枚举常量的枚举类型相对应的 Class 对象。

3)String name()

返回此枚举常量的名称,在其枚举声明中对其进行声明。

4)int ordinal()

返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。

5)String toString()

返回枚举常量的名称,它包含在声明中。

三、枚举的使用

用法一:常量

public static enum EnumNumbers {
Red, Blue, Green;
};


Log.d(TAg, "---------输出:名称,序数----------");
for (EnumNumbers e : EnumNumbers.values()) {
Log.d(TAg, "ordinal = "+e.ordinal()+", name = "+e.name());
}


02-02 14:35:04.396: D/wxx(319): ---------输出:名称,序数----------
02-02 14:35:04.396: D/wxx(319): ordinal = 0, name = Red
02-02 14:35:04.396: D/wxx(319): ordinal = 1, name = Blue
02-02 14:35:04.396: D/wxx(319): ordinal = 2, name = Green


用法二:自定义构造方法

public static enum EnumNumbers {
Red(3), Blue(2), Green(1);

public final int num;
private EnumNumbers(int num) {
this.num = num;
}
};


Log.d(TAg, "---------输出:名称,序数,赋值----------");
for (EnumNumbers e : EnumNumbers.values()) {
Log.d(TAg, "ordinal = "+e.ordinal()+", name = "+e.name()+", num = "+e.num);
}


02-02 14:41:46.891: D/wxx(4475): ---------输出:名称,序数,赋值----------
02-02 14:41:46.891: D/wxx(4475): ordinal = 0, name = Red, num = 3
02-02 14:41:46.891: D/wxx(4475): ordinal = 1, name = Blue, num = 2
02-02 14:41:46.891: D/wxx(4475): ordinal = 2, name = Green, num = 1


由输出结果可以看出,顺序数ordinal是不变的,都是从0开始的整数~~

构造函数类型必须为private ~~

用法三:自定义方法

public static enum EnumNumbers {
Red(3, "红色"), Blue(2, "蓝色"), Green(1, "绿色");

public final int num;
public final String des;
private EnumNumbers(int num, String des) {
this.num = num;
this.des = des;
}

public int getNum() {
return this.num;
}

public String getDes() {
return this.des;
}
};


Log.d(TAg, "---------输出:方法取值----------");
for (EnumNumbers e : EnumNumbers.values()) {
Log.d(TAg, "ordinal = "+e.ordinal()+", name = "+e.name()+", getNum = "+e.getNum()+", getDes = "+e.getDes());
}


02-02 14:50:49.846: D/wxx(9504): ---------输出:方法取值----------
02-02 14:50:49.846: D/wxx(9504): ordinal = 0, name = Red, getNum = 3, getDes = 红色
02-02 14:50:49.846: D/wxx(9504): ordinal = 1, name = Blue, getNum = 2, getDes = 蓝色
02-02 14:50:49.846: D/wxx(9504): ordinal = 2, name = Green, getNum = 1, getDes = 绿色


用法四:实现接口

public interface myInterface {
int getNum();
String getDes();
}

public static enum EnumNumbers implements myInterface {
Red(3, "红色"), Blue(2, "蓝色"), Green(1, "绿色");

public final int num;
public final String des;
private EnumNumbers(int num, String des) {
this.num = num;
this.des = des;
}

@Override
public int getNum() {
// TODO Auto-generated method stub
return this.num;
}

@Override
public String getDes() {
// TODO Auto-generated method stub
return this.des;
}
};
</pre><p></p><p><span style="font-size:18px"></span></p><pre code_snippet_id="1572822" snippet_file_name="blog_20160202_12_9691238" name="code" class="html">		Log.d(TAg, "---------输出:实现接口----------");
for (EnumNumbers e : EnumNumbers.values()) {
Log.d(TAg, "ordinal = "+e.ordinal()+", name = "+e.name()+", getNum = "+e.getNum()+", getDes = "+e.getDes());
}


02-02 15:02:38.261: D/wxx(16830): ---------输出:实现接口----------
02-02 15:02:38.266: D/wxx(16830): ordinal = 0, name = Red, getNum = 3, getDes = 红色
02-02 15:02:38.266: D/wxx(16830): ordinal = 1, name = Blue, getNum = 2, getDes = 蓝色
02-02 15:02:38.266: D/wxx(16830): ordinal = 2, name = Green, getNum = 1, getDes = 绿色
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: