最近又看了一遍JAVA编程思想。要不是我之前有看书画线的习惯。我真的有怀疑我之前居然TMD的看过这本书。
昨天晚上看到了ENUM这个章节。之前这个特性一直用的很少,因为只知道它常量的特性,不过一般都用final static来替代了,也很少再去打开一个类来定义一段字段。
首先java ENUM是1.5之后才有的特性,一般用来一组有相同特性的相同类型的常量。使用方法很简单,定义:
public enum DynElement {
TEXT,
DYNAMIC,
ISEQUAL,
ISNOTEQUAL,
ISGREATTHAN,
ISGREATEQUAL,
ISLESSTHAN,
ISLESSEQUEAL,
ISPROAVAILABLE,
ISNOTPROAVAILABLE,
ISNULL,
ISNOTNULL,
ISEMPTY,
ISNOTEMPTY,
ISPARAMETERPRESENT,
ISNOTPARAMETERPRESENT,
ITERATE;
}
public enum DynElement {
TEXT,
DYNAMIC,
ISEQUAL,
ISNOTEQUAL,
ISGREATTHAN,
ISGREATEQUAL,
ISLESSTHAN,
ISLESSEQUEAL,
ISPROAVAILABLE,
ISNOTPROAVAILABLE,
ISNULL,
ISNOTNULL,
ISEMPTY,
ISNOTEMPTY,
ISPARAMETERPRESENT,
ISNOTPARAMETERPRESENT,
ITERATE;
}
调用的时候直接enum名称点常量就可以了
public static void main(String args[]){
System.out.println(DynElement.TEXT);
}
public static void main(String args[]){
System.out.println(DynElement.TEXT);
}
另外枚举也可以自定义变量的值
public enum WeekDay {
Mon("Monday"),
Tue("Tuesday"),
Wed("Wednesday"),
Thu("Thursday"),
Fri("Friday"),
Sat("Saturday"),
Sun("Sunday");
/**定义枚举类型自己的属性**/
private final String day;
private WeekDay(String day) {
this.day = day;
}
/**定义枚举类型自己的方法**/
public static void printDay(int i){
switch(i){
case 1: System.out.println(WeekDay.Mon); break;
case 2: System.out.println(WeekDay.Tue);break;
case 3: System.out.println(WeekDay.Wed);break;
case 4: System.out.println(WeekDay.Thu);break;
case 5: System.out.println(WeekDay.Fri);break;
case 6: System.out.println(WeekDay.Sat);break;
case 7: System.out.println(WeekDay.Sun);break;
default:System.out.println("wrong number!");
}
}
public String getDay() {
return day;
}
}
public enum WeekDay {
Mon("Monday"),
Tue("Tuesday"),
Wed("Wednesday"),
Thu("Thursday"),
Fri("Friday"),
Sat("Saturday"),
Sun("Sunday");
/**定义枚举类型自己的属性**/
private final String day;
private WeekDay(String day) {
this.day = day;
}
/**定义枚举类型自己的方法**/
public static void printDay(int i){
switch(i){
case 1: System.out.println(WeekDay.Mon); break;
case 2: System.out.println(WeekDay.Tue);break;
case 3: System.out.println(WeekDay.Wed);break;
case 4: System.out.println(WeekDay.Thu);break;
case 5: System.out.println(WeekDay.Fri);break;
case 6: System.out.println(WeekDay.Sat);break;
case 7: System.out.println(WeekDay.Sun);break;
default:System.out.println("wrong number!");
}
}
public String getDay() {
return day;
}
}
测试方法为:
public static void main(String args[]) {
for (WeekDay day : WeekDay.values()) {
System.out.println(day + "====>" + day.getDay());
}
WeekDay.printDay(5);
}
public static void main(String args[]) {
for (WeekDay day : WeekDay.values()) {
System.out.println(day + "====>" + day.getDay());
}
WeekDay.printDay(5);
}
输出结果为:
Mon====>Monday
Tue====>Tuesday
Wed====>Wednesday
Thu====>Thursday
Fri====>Friday
Sat====>Saturday
Sun====>Sunday
Fri
其实枚举就是把这些常量编译成了static final类型的
比如上面的例子就是
public static final WeekDay Mon;
public static final WeekDay Tue;
public static final WeekDay Wed;
public static final WeekDay Thu;
public static final WeekDay Fri;
public static final WeekDay Sat;
public static final WeekDay Sun;
public static final WeekDay Mon;
public static final WeekDay Tue;
public static final WeekDay Wed;
public static final WeekDay Thu;
public static final WeekDay Fri;
public static final WeekDay Sat;
public static final WeekDay Sun;
所以实际上 Enum 类型就是以 Java 类来实现的,没有什么新的特点,只不过 java 编译器帮我们做了语法的解析和编译。完全也可以自己实现。但是既然有这样方便一个东西,当然会去用了。
这里请注意下Weekday这个方法是private,定义成public是会报错的。其实主要防止其他类再次定义枚举类
另外枚举的构造器只在构造枚举的时候被用到
枚举继承了ENUM类,有以一下几个方法:
A.ordinal()用来返回枚举值在枚举类中的顺序。
B.compareTo ()比较两个枚举值的顺序,返回顺序之差。
C.values(),静态方法,返回包含一个全部枚举值的数组。
D.toString(),返回枚举常量的名称
E.valueOf(),返回指定没弄成的枚举类型的枚举常量。
另外下面我们在举一个枚举的使用例子:
Color color=Color.RED;
switch(color){
case RED: System.out.println("it's red");break;
case BLUE: System.out.println("it's blue");break;
case BLACK: System.out.println("it's blue");break;
}
Color color=Color.RED;
switch(color){
case RED: System.out.println("it's red");break;
case BLUE: System.out.println("it's blue");break;
case BLACK: System.out.println("it's blue");break;
}
枚举可以用来做switch,switch是支持char和int的哦