java enum枚举类的用法以及高级玩法
enum(枚举)类介绍
**java枚举类是一组预定义常量的集合,使用enum关键字声明这个类,常量名称官方建议大写 **
1.enum类基本用法
举一个常见的例子,例如星期就可以描述为一个枚举类,如下
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY
}
怎么调用呢,Java switch语句参数就支持enum类
public class EnumTest {
Day day;
public EnumTest(Day day) {
this.day = day;
}
public void tellItLikeItIs() {
switch (day) {
case MONDAY:
System.out.println("周一各种不在状态");
break;
case FRIDAY:
System.out.println("周五感觉还不错");
break;
case SATURDAY: case SUNDAY:
System.out.println("周末给人的感觉是最棒的");
break;
default:
System.out.println("周内感觉就那样吧。。。");
break;
}
}
public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
thirdDay.tellItLikeItIs();
EnumTest fifthDay = new EnumTest(Day.FRIDAY);
fifthDay.tellItLikeItIs();
EnumTest sixthDay = new EnumTest(Day.SATURDAY);
sixthDay.tellItLikeItIs();
EnumTest seventhDay = new EnumTest(Day.SUNDAY);
seventhDay.tellItLikeItIs();
}
}
运行结果
2.enum类和static常量区别
看了上面的例子,有些小伙伴就嗤之以鼻孔了,这没什么嘛,java静态常量也一样可以办到
public class EnumTest2 {
public static final int MONDAY= 1;
public static final int WEDNESDAY= 3;
public static final int FRIDAY= 5;
public static final int SATURDAY= 6;
public static final int SUNDAY= 7;
public void tellItLikeItIs(int day) {
switch (day) {
case 1:
System.out.println("周一各种不在状态");
break;
case 5:
System.out.println("周五感觉还不错");
break;
case 6: case 7:
System.out.println("周末给人的感觉是最棒的");
break;
default:
System.out.println("周内感觉就那样吧。。。");
break;
}
}
public static void main(String[] args) {
EnumTest2 firstDay = new EnumTest2();
firstDay.tellItLikeItIs(MONDAY);
EnumTest2 thirdDay = new EnumTest2();
thirdDay.tellItLikeItIs(WEDNESDAY);
EnumTest2 fifthDay = new EnumTest2();
fifthDay.tellItLikeItIs(FRIDAY);
EnumTest2 sixthDay = new EnumTest2();
sixthDay .tellItLikeItIs(SATURDAY);
EnumTest2 seventhDay = new EnumTest2();
seventhDay.tellItLikeItIs(SUNDAY);
}
}
在这个使用方式效果上来看是没有区别的,其实enum类的每一个枚举值也是 static final的,但是我们为什么要选择使用enum枚举类呢
1.static方式的静态变量类型不安全,我们可以在调用的时候传入其他值,导致错误
例如: seventhDay.tellItLikeItIs(999);
2.static方式的静态变量不支持属性扩展,每一个key对应一个值,而enum的每一个key可以拥有自己的属性
3.enum类自定义属性
这就是enum比static静态变量好用的地方了,可以赋予每一个枚举值若干个属性,例如
public enum Day {
MONDAY(1, "星期一", "星期一各种不在状态"),
TUESDAY(2, "星期二", "星期二依旧犯困"),
WEDNESDAY(3, "星期三", "星期三感觉半周终于过去了"),
THURSDAY(4, "星期四", "星期四期待这星期五"),
FRIDAY(5, "星期五", "星期五感觉还不错"),
SATURDAY(6, "星期六", "星期六感觉非常好"),
SUNDAY(7, "星期日", "星期日感觉周末还没过够。。。");
Day(int index, String name, String value) {
this.index = index;
this.name = name;
this.value = value;
}
private int index;
private String name;
private String value;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
这次测试类就有些些变化了,更灵活了
public class EnumTest {
Day day;
public EnumTest(Day day) {
this.day = day;
}
public void tellItLikeItIs() {
switch (day) {
case MONDAY:
System.out.println(day.getName()+day.getValue());
break;
case FRIDAY:
System.out.println(day.getName()+day.getValue());
break;
case SATURDAY: case SUNDAY:
System.out.println(day.getName()+day.getValue());
break;
default:
System.out.println(day.getName()+day.getValue());
break;
}
}
public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
thirdDay.tellItLikeItIs();
EnumTest fifthDay = new EnumTest(Day.FRIDAY);
fifthDay.tellItLikeItIs();
EnumTest sixthDay = new EnumTest(Day.SATURDAY);
sixthDay.tellItLikeItIs();
EnumTest seventhDay = new EnumTest(Day.SUNDAY);
seventhDay.tellItLikeItIs();
}
}
执行结果
4.enum类高级玩法
你以为enum这样就完了么,还有更好玩的东西,每一个enum枚举值还可以拥有各自的内部方法!二话不说,上栗子
public enum Day {
MONDAY(1, "星期一", "各种不在状态"){
@Override
public Day getNext() {
return TUESDAY;
}
},
TUESDAY(2, "星期二", "依旧犯困"){
@Override
public Day getNext() {
return WEDNESDAY;
}
},
WEDNESDAY(3, "星期三", "感觉半周终于过去了"){
@Override
public Day getNext() {
return THURSDAY;
}
},
THURSDAY(4, "星期四", "期待这星期五"){
@Override
public Day getNext() {
return FRIDAY;
}
},
FRIDAY(5, "星期五", "感觉还不错"){
@Override
public Day getNext() {
return SATURDAY;
}
},
SATURDAY(6, "星期六", "感觉非常好"){
@Override
public Day getNext() {
return SUNDAY;
}
},
SUNDAY(7, "星期日", "感觉周末还没过够。。。"){
@Override
public Day getNext() {
return MONDAY;
}
};
Day(int index, String name, String value) {
this.index = index;
this.name = name;
this.value = value;
}
private int index;
private String name;
private String value;
public abstract Day getNext();
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void