首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

浅谈J2SE 枚举部类

2012-10-26 
浅谈J2SE 枚举类型public class TestEnum { /*** @param args*/ public static void main(String[] args)

浅谈J2SE 枚举类型
public class TestEnum { /** * @param args */ public static void main(String[] args) { new TestEnum().work(Week.SUN); System.out.println("*****************************"); System.out.println("得到Week枚举类型中每个常量的常量值"); System.out.println(Week.MON.ordinal()); System.out.println(Week.TUE.ordinal()); System.out.println(Week.WED.ordinal()); System.out.println(Week.THU.ordinal()); System.out.println(Week.FRI.ordinal()); System.out.println(Week.SAT.ordinal()); System.out.println(Week.SUN.ordinal()); System.out.println("*****************************"); new TestEnum().workSwitch(Week.FRI); } public void work(Week day) { if (day.equals(Week.SAT)) { System.out.println("Go shopping!"); } else if (day.equals(Week.SUN)) { System.out.println("Have a Rest"); } else { System.out.println("Work"); } } public void workSwitch(Week day) { switch (day) { case MON: case TUE: case WED: case THU: case FRI: System.out.println("大家辛苦了,请努力工作!"); break; case SAT: System.out.println("星期六,一起去购物!"); break; case SUN: System.out.println("星期天,大家休息!"); break; default: System.out.println("搞错了!"); break; } }}

?

?

运行效果:

Have a Rest
*****************************
得到Week枚举类型中每个常量的常量值
0
1
2
3
4
5
6
*****************************
大家辛苦了,请努力工作!

热点排行