Enum枚举类的学习笔记
小案例:
你写了一个小程序,不过好久不用了,突然有一天,你想使用一下它。程序要想正确运行,需要将今天星期几存到数据库里。这个时候,你开始犯难了。
当初的你还很年轻,不懂程序界的险恶,设计这个程序的时候,傻不拉几把这个字段设计为int类型的,用0代表周日,1代表周一。。。6代表周六,添加的时候就setWeekday(0)。但是这么长时间没用了,你忘记自己是从周一开始计算还是周日开始计算了,换句话说,你想不起来0代表的是周一还是周日了!
于是你各种翻代码,看数据库的字段,数据库保存的信息,终于搞懂了,你很开心,用了一次之后,觉得这个程序没意思,又不用了。
很久之后,你心血来潮,又想用一次它,很不幸,你又忘记到底0代表周一还是周日了,一番查找之后。你决定重构代码,因为你受不了了!!
-
静态变量来帮忙
经过一番思考,你决定使用七个静态变量来代表星期几,以后只要引用和静态变量就可以了,而不用自己输入012….你这么写:
1 public class Weekday { 2 public final static int SUN = 0; 3 public final static int MON = 1; 4 public final static int TUE = 2; 5 public final static int WED = 3; 6 public final static int THU = 4; 7 public final static int FRI = 5; 8 public final static int SAT = 6; 9 10 }
机智如你,这个时候,只要 Weekday.SUN 就可以了,不用操心到底应该填写0还是填写1。
但是这个时候的你,也不是当初初出茅庐的小伙子了,很明显,这样写已经不能满足你了。你还想让这个类做更多的事,
比如,你想知道下一天是星期几,还想把今天是星期几打印出来。一番深思熟虑后,你改成了这样:
1 public class Weekday { 2 3 private Weekday(){} 4 5 public final static Weekday SUN = new Weekday(); 6 public final static Weekday MON = new Weekday(); 7 public final static Weekday TUE = new Weekday(); 8 public final static Weekday WED = new Weekday(); 9 public final static Weekday THU = new Weekday(); 10 public final static Weekday FRI = new Weekday(); 11 public final static Weekday SAT = new Weekday(); 12 13 public static Weekday getNextDay(Weekday nowDay){ 14 if(nowDay == SUN) { 15 return MON; 16 }else if(nowDay == MON) { 17 return TUE; 18 }else if(nowDay == TUE) { 19 return WED; 20 }else if(nowDay == WED) { 21 return THU; 22 }else if(nowDay == THU) { 23 return FRI; 24 }else if(nowDay == FRI) { 25 return SAT; 26 }else { 27 return SUN; 28 } 29 } 30 31 public static void printNowDay(Weekday nowDay){ 32 if(nowDay == SUN) 33 System.out.println("sunday"); 34 else if(nowDay == MON) 35 System.out.println("monday"); 36 else if(nowDay == TUE) 37 System.out.println("tuesday"); 38 else if(nowDay == WED) 39 System.out.println("wednesday"); 40 else if(nowDay == THU) 41 System.out.println("thursday"); 42 else if(nowDay == FRI) 43 System.out.println("friday"); 44 else 45 System.out.println("saturday"); 46 } 47 48 } 49 50 class Test{ 51 public static void main(String[] args) { 52 Weekday nowday = Weekday.SUN; 53 Weekday.printNowDay(nowday); 54 Weekday nextDay = Weekday.getNextDay(nowday); 55 System.out.print("nextday ====> "); 56 Weekday.printNowDay(nextDay); 57 } 58 } 59 //测试结果: 60 //sunday 61 //nextday ====> monday
哟,不错。考虑的很详细。并且私有构造方法后,外界就不能创建该类的对象了,这样就避免了星期八星期九的出现,所有Weekday的对象都在该类内部创建。
不对,好像缺了点什么,我要的是int!我的int呢?!。所以,你还需要一个这样的方法:
1 public static int toInt(Weekday nowDay){ 2 if(nowDay == SUN) 3 return 0; 4 else if(nowDay == MON) 5 return 1; 6 else if(nowDay == TUE) 7 return 2; 8 else if(nowDay == WED) 9 return 3; 10 else if(nowDay == THU) 11 return 4; 12 else if(nowDay == FRI) 13 return 5; 14 else 15 return 6; 16 }
- 简单的枚举类
1 public enum Weekday { 2 SUN,MON,TUS,WED,THU,FRI,SAT 3 }
代码这么少?
没错,这就是枚举类,我们来看看怎么使用它:
1 class Test2{ 2 public static void main(String[] args) { 3 Weekday sun = Weekday.SUN; 4 System.out.println(sun); // 输出 SUN 5 } 6 }
看起来和上面的静态变量使用方式差不多,而且默认的toString方法返回的就是对应的名字。
我们上面的那段代码重写toString也是不可以打印出当前是星期几的,因为toString方法没有参数。所以我们自己写了一个printNowDay方法。
当然,这么简单的枚举类是不可能实现我们的要求的,所以,我们还要接着写:
1 public enum Weekday { 2 SUN(0),MON(1),TUS(2),WED(3),THU(4),FRI(5),SAT(6); 3 4 private int value; 5 6 private Weekday(int value){ 7 this.value = value; 8 } 9 10 public static Weekday getNextDay(Weekday nowDay){ 11 int nextDayValue = nowDay.value; 12 13 if (++nextDayValue == 7){ 14 nextDayValue =0; 15 } 16 17 return getWeekdayByValue(nextDayValue); 18 } 19 20 public static Weekday getWeekdayByValue(int value) { 21 for (Weekday c : Weekday.values()) { 22 if (c.value == value) { 23 return c; 24 } 25 } 26 return null; 27 } 28 } 29 30 class Test2{ 31 public static void main(String[] args) { 32 System.out.println("nowday ====> " + Weekday.SAT); 33 System.out.println("nowday int ====> " + Weekday.SAT.ordinal()); 34 System.out.println("nextday ====> " + Weekday.getNextDay(Weekday.SAT)); // 输出 SUN 35 36 //输出: 37 //nowday ====> SAT 38 //nowday int ====> 6 39 //nextday ====> SUN 40 } 41 }