java enum枚举类
enum(枚举)类介绍
**java枚举类是一组预定义常量的集合,使用enum关键字声明这个类,常量名称官方建议大写 **
1.enum类基本用法
举一个常见的例子,例如星期就可以描述为一个枚举类,如下
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY
}
怎么调用呢,Java switch语句参数就支持enum类
/**
* @author zn
* @date 2020/3/30
**/
public class Test {
public static void select(Day day) {
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) {
select(Day.MONDAY);
}
}
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 static void select(int day) {
switch (day) {
case EnumTest2.MONDAY:
System.out.println("周一各种不在状态");
break;
case EnumTest2.FRIDAY:
System.out.println("周五感觉还不错");
break;
case EnumTest2.SATURDAY: case EnumTest2.SUNDAY:
System.out.println("周末给人的感觉是最棒的");
break;
default:
System.out.println("周内感觉就那样吧。。。");
break;
}
}
public static void main(String[] args) {
select(EnumTest2.MONDAY);
}
}
在这个使用方式效果上来看是没有区别的,其实enum类的每一个枚举值也是 static final的,但是我们为什么要选择使用enum枚举类呢
1.static方式的静态变量类型不安全,我们可以在调用的时候传入其他值,导致错误
例如: select(12);
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;
}
}
这次测试类就有些些变化了,更灵活了
/**
* @author zn
* @date 2020/3/30
**/
public class Test {
public static void select(Day day) {
switch (day) {
case MONDAY:
case FRIDAY:
case SATURDAY:
case SUNDAY:
System.out.println(day.getName()+day.getValue());
break;
default:
System.out.println("周内感觉就那样吧。。。");
break;
}
}
public static void main(String[] args) {
select(Day.MONDAY);
}
}
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 setValue(String value) {
this.value = value;
}
}
调用下看看
public class EnumTest {
public static void select(Day day) {
switch (day) {
case MONDAY:
System.out.println(day.getName()+day.getValue());
System.out.println(day.getName()+"的下一天是"+day.getNext().getName());
break;
case FRIDAY:
System.out.println(day.getName()+day.getValue());
System.out.println(day.getName()+"的下一天是"+day.getNext().getName());
break;
case SATURDAY: case SUNDAY:
System.out.println(day.getName()+day.getValue());
System.out.println(day.getName()+"的下一天是"+day.getNext().getName());
break;
default:
System.out.println(day.getName()+day.getValue());
System.out.println(day.getName()+"的下一天是"+day.getNext().getName());
break;
}
}
public static void main(String[] args) {
select(Day.MONDAY);
}
}
网友评论