日期:2014-05-20  浏览次数:20668 次

浅谈J2SE 枚举类型
public enum Week {
MON, TUE, WED, THU, FRI, SAT, SUN
}

?

测试:

?

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
*****************************
大家辛苦了,请努力工作!