1、枚举的作用
如果程序运行时需要的数据只能是一定范围内的值,jdk5之前我们可以采用自定义类来解决,jdk5后就可以直接采用枚举来解决了。
例如要限定操作类型只能为增删改查四种,我们可以用自定义类的方式解决如下:
public class Operation
{
private String operType;
private Operation(String operType)
{
this.operType = operType;
}
public static final Operation ADD = new Operation("add");
public static final Operation DELETE = new Operation("delete");
public static final Operation MODIFY= new Operation("modify");
public static final Operation SELECT = new Operation("select");
public String getOperType()
{
return operType;
}
}
jdk5后我们就可以通过枚举来更简洁的解决问题:
public enum OperationByEnum
{
ADD("add"), DELETE("delete"), MODIFY("modify"), SELECT("select");
public String getOperType()
{
return operType;
}
private String operType;
private OperationByEnum(String operType)
{
this.operType = operType;
}
}
查看上述两个类的class文件可以看出两种方式可以认为是等价的,
D:\java\workspace\EnumDemo\bin>javap OperationByEnum.class
Compiled from "OperationByEnum.java"
public final class OperationByEnum extends java.lang.Enum<OperationByEnum> {
public static final OperationByEnum ADD;
public static final OperationByEnum DELETE;
public static final OperationByEnum MODIFY;
public static final OperationByEnum SELECT;
static {};
public java.lang.String getOperType();
public static OperationByEnum[] values();
public static OperationByEnum valueOf(java.lang.String);
}
D:\java\workspace\EnumDemo\bin>javap Operation.class
Compiled from "Operation.java"
public class Operation {
public static final Operation ADD;
public static final Operation DELETE;
public static final Operation MODIFY;
public static final Operation SELECT;
static {};
public java.lang.String getOperType();
}
也就是说现在我们定义一个enum类型后,编译器自动帮我们实现一个构造函数为private自定义类,再也不用我们自己去实现一个类似于图1中的自定义类了。
2、带抽象方法的枚举
public class EnumTest {
public static void main(String[] args)
{
print(OperationByEnum.ADD);
}
public static void print(OperationByEnum arg)
{
System.out.println(arg.getOperType());
}
}
输出 add
每个枚举值的获取操作类型方法此时获取的是英文名称,如果我想添加一个方法来获取每个操作类型的中文名称,应该怎么处理呢?此时带抽象方法的枚举就要闪亮登场了
public enum OperationByEnum
{
ADD("add")
{
@Override
public String getChineseName()
{
return "增";
}
},
DELETE("delete")
{
@Override
public String getChineseName()
{
return "删";
}
},
MODIFY("modify")
{
@Override
public String getChineseName()
{
return "改";
}
},
SELECT("select")
{
@Override
public String getChineseName()
{
return "查";
}
};
private String operType;
public String getOperType()
{
return operType;
}
private OperationByEnum(String operType)
{
this.operType = operType;
}
public abstract String getChineseName();
}
此时就可以通过getChineseName来获取每个枚举值的中文名称了。
3、枚举类的常用API
所有枚举类均继承自java.lang.Enum类,其中比较常用的方法为:
下面的例子可以帮助我们理解valueOf方法的作用
String str = "ADD";
OperationByEnum ob = OperationByEnum.valueOf(str);
System.out.println(ob == OperationByEnum.ADD);//TRUE
String str2="add";
OperationByEnum ob1 = OperationByEnum.valueOf(str2);
//Exception in thread "main" java.lang.IllegalArgumentException: No enum constant OperationByEnum.add
另外所有枚举类都会有一个values()方法,用于返回枚举的所有值
OperationByEnum[] array = OperationByEnum.values();
for(OperationByEnum ob:array)
{
System.out.println(ob.getChineseName());
}
注:
Where does the Enum.valueOf(String) method come from?
/**
* Returns an array containing the constants of this enum
* type, in the order they're declared. This method may be
* used to iterate over the constants as follows:
*
* for(E c : E.values())
* System.out.println(c);
*
* @return an array containing the constants of this enum
* type, in the order they're declared
*/
public static E[] values();
/**
* Returns the enum constant of this type with the specified
* name.
* The string must match exactly an identifier used to declare
* an enum constant in this type. (Extraneous whitespace
* characters are not permitted.)
*
* @return the enum constant with the specified name
* @throws IllegalArgumentException if this enum type has no
* constant with the specified name
*/
public static E valueOf(String name);
4、枚举的特性总结
枚举类是一种特殊的JAVA类,枚举类中每声明一个枚举值就代表枚举类的一个实例对象。
与JAVA普通类一样,声明枚举类时也可以声明类的属性、方法、构造函数,但构造函数必须为私有。
枚举类也可以实现接口,继承抽象类。可以作为switch语句的参数。
若枚举类只有一个枚举值,则可以当做单例设计模式使用。
来源:oschina
链接:https://my.oschina.net/u/999023/blog/665398