编辑
2023-03-09
学习记录
00
请注意,本文编写于 756 天前,最后修改于 749 天前,其中某些信息可能已经过时。

目录

enum(枚举)类介绍🥥 🍑
1.enum类基本用法
运行结果
2.enum类和static常量区别
3.enum类自定义属性
运行结果
4.enum类当成字典对照
运行结果
5.enum类高级玩法
运行结果

enum(枚举)类介绍🥥 🍑

java枚举类是一组预定义常量的集合,使用enum关键字声明这个类,常量名称官方建议大写

1.enum类基本用法

举一个常见的例子,例如星期就可以描述为一个枚举类,如下

java
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY }

怎么调用呢,Java switch语句参数就支持enum类

java
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(); } }

运行结果

或者

通过名称获取对应的值

java
public enum ReturnEnum { patientSex("PATIENT_SEX"), submitTime("submit_Time"), submitDoctor("submit_Doctor"), deptDischargeName("dept_Discharge_Name"), returnCount("return_count"), dischargeTime("discharge_Time"), diagnosis("diagnosis"); public String column; ReturnEnum(String column) { this.column=column; } public String getColumn(){ return column; } public static String getColumnByName(String column){ for (ReturnEnum returnEnum:ReturnEnum.values()){ if (returnEnum.name().equals(column)){ return returnEnum.getColumn(); } } return null; }

使用

System.out.println(ReturnEnum.getColumn("diagnosis"));

2.enum类和static常量区别

看了上面的例子,有些小伙伴就嗤之以鼻孔了,这没什么嘛,java静态常量也一样可以办到

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静态变量好用的地方了,可以赋予每一个枚举值若干个属性,例如

java
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; } }

这次测试类就有些些变化了,更灵活了

java
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(); } }

运行结果

image.png

4.enum类当成字典对照

public enum Day { MONDAY("星期一",1), TUESDAY("星期二",1), WEDNESDAY("星期三",1),THURSDAY("星期四",1), FRIDAY("星期五",1), SATURDAY("星期六",1), SUNDAY("星期日",1); private String dayName; private Integer dayNum; private Day(String dayName,Integer dayNum){ this.dayName=dayName; this.dayNum=dayNum; } public static String getDayName(int dayNum){ for (Day day:Day.values()){ if (day.dayNum==dayNum){ return day.getDayName(); } } return null; } public static Integer getDayNum(String dayName){ for (Day day:Day.values()){ if (day.getDayName().equals(dayName)){ return day.getDayNum(); } } return null; } public String getDayName() { return dayName; } public void setDayName(String dayName) { this.dayName = dayName; } public Integer getDayNum() { return dayNum; } public void setDayNum(Integer dayNum) { this.dayNum = dayNum; } }

测试类

public class EnumTest { public static void main(String[] args) { System.out.println(Day.getDayName(1)); System.out.println(Day.getDayNum("星期二")); } }

运行结果

image.png

5.enum类高级玩法

你以为enum这样就完了么,还有更好玩的东西,每一个enum枚举值还可以拥有各自的内部方法!二话不说,上栗子

java
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; } }

调用下看看

java
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()); 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) { 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(); } }

运行结果

image.png

本文作者:Weee

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!