Android中枚举enum的使用
来源:互联网 发布:.space是什么域名 编辑:程序博客网 时间:2024/05/18 11:48
一、枚举说明和注意:
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 = Red02-02 14:35:04.396: D/wxx(319): ordinal = 1, name = Blue02-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 = 302-02 14:41:46.891: D/wxx(4475): ordinal = 1, name = Blue, num = 202-02 14:41:46.891: D/wxx(4475): ordinal = 2, name = Green, num = 1
构造函数类型必须为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;}@Overridepublic int getNum() {// TODO Auto-generated method stubreturn this.num;}@Overridepublic String getDes() {// TODO Auto-generated method stubreturn 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 = 绿色
0 0
- Android中枚举enum的使用
- Android 中如何使用 enum / 枚举
- java中Enum(枚举)的使用
- java中枚举enum的使用
- Java中枚举(Enum)的使用
- enum 枚举的使用
- 枚举(enum)的使用
- 【Enum】枚举的使用
- 枚举Enum的使用
- Enum枚举的使用
- enum 枚举的使用
- Android是否推荐使用枚举(enum)?使用枚举的利弊以及枚举倒底占多少内存?
- java enum枚举的使用
- Enum枚举类型的使用
- java 枚举的使用 enum
- enum枚举类型的使用
- 枚举类型ENUM的使用
- 枚举(enum)的使用
- HTML提交与点击的区别
- 视频播放的基本原理
- Android官方数据绑定框架DataBinding
- Windows + IDEA + SBT 打造Spark源码阅读环境
- windows下用Eclipse编译运行android原生浏览器
- Android中枚举enum的使用
- 【SCOI2013】【BZOJ3323】多项式的运算
- HashMap
- [树状数组+上升子序列] HDU 3030 Increasing Speed Limits
- 在listView适配器的内存优化给点击事件带来的麻烦
- 普通内部类详解
- c++ builder TADOQuery控件的type mismatch for field‘note',expecting: String actual: Memo.
- RMQ算法
- 258Add Digits