Java枚举
⼀、枚举
概念
枚举类型是Java 5中新增特性的⼀部分,它是⼀种特殊的数据类型,之所以特殊是因为它既是⼀种类(class)类型却⼜⽐类类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁性、安全性以及便捷性。
枚举在各个语⾔当中都有着⼴泛的应⽤,通常⽤来表示诸如颜⾊、⽅式、类别、状态等等数⽬有限、形式离散、表达⼜极为明确的量。Java从JDK5开始,引⼊了对枚举的⽀持。
为什么使⽤枚举
在枚举出现之前,如果想要表示⼀组特定的离散值,往往使⽤⼀些常量。
例如:
public class Entity {
public static final int VIDEO = 1;//视频
public static final int AUDIO = 2;//⾳频
public static final int TEXT = 3;//⽂字
public static final int IMAGE = 4;//图⽚
private int id;
private int type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
例如,针对上述的Entity类,如果要对Entity对象的type属性进⾏赋值,⼀般会采⽤如下⽅法:
Entity e = new Entity();
e.setId(10);
//e.setType(2);
//⽽这样的话,问题⼜来了。这样做,客户端必须对这些常量去建⽴理解,才能了解如何去使⽤这个东
⻄。
//说⽩了,在调⽤的时候,如果⽤户不到Entity类中去看看,还真不知道这个参数应该怎么传、怎
么调
e.setType(Entity.AUDIO);
使⽤枚举
枚举(在Jave中简称为enum)是⼀个特定类型的类。所有枚举都是Java中的新类java.lang.Enum的隐式⼦类。此类不能⼿⼯进⾏⼦类定义。⼀个简单的枚举可以是这样:
public enum TypeEnum {
VIDEO,AUDIO,TEXT,IMAGE;
}
上⾯的Entity类就可以改成这样:
public class Entity2 {
private int id;
private TypeEnum type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public TypeEnum getType() {
return type;
}
public void setType(TypeEnum type) {
this.type = type;
}
}
在为Entity对象赋值的时候,就可以这样:
Entity2 e = new Entity2();
e.setId(10);
e.setType(TypeEnum.AUDIO);
在Java中⼀个枚举就是⼀个类,它也可以有属性和⽅法,并且实现接⼝。只是所有的枚举都继承⾃java.lang.Enum类,因此enum不可以再继承其他的类。
下⾯给出在枚举中声明属性和⽅法的示例:
public enum TypeEnum {
VIDEO(1), AUDIO(2), TEXT(3), IMAGE(4);
int value;
TypeEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
如果要为每个枚举值指定属性,则在枚举中必须声明⼀个参数为属性对应类型的构造⽅法(不能是public)
public enum TypeEnum {
VIDEO(1, "视频"), AUDIO(2, "⾳频"), TEXT(3, "⽂本"), IMAGE(4, "图像");
int value;
String name;
TypeEnum(int value, String name) {
this.value = value;
this.name = name;
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
public static void main(String[] args) {
System.out.println(TypeEnum.VIDEO.name);
System.out.println(TypeEnum.VIDEO.value);
}
}
枚举遍历读取属性
public static void main(String[] args) {
TypeEnum[] values = TypeEnum.values();
for (TypeEnum typeEnum:values) {
System.out.println(typeEnum);
}
}