package com.zj.tool; public enum WeekDay { Mon("Monday",1), Tue("Tuesday",2), Wed("Wednesday",3), Thu("Thursday",4), Fri("Friday",5), Sat("Saturday",6), Sun("Sunday",7); /**定义枚举类型自己的属性**/ private final String day; private final int dayNum; private WeekDay(String day,int dayNum) { this.day = day; this.dayNum = dayNum; } public int getDayNum() { return dayNum; } /**定义枚举类型自己的方法**/ 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()+ "====>" + day.getDayNum()); } WeekDay.printDay(5); } }
一个很实际的问题,数据库里面存的是数字但是页面要显示字符串,此时就i显示出enum的好处了
时间: 2024-10-29 10:43:35