Java1.5新特性Enum列举的用法(二)
文章作者 100test 发表时间 2007:03:14 16:49:33
来源 100Test.Com百考试题网
Enum的属性调用:
  下面的代码展示了调用enum对象的方法,这也是它通常的用法:
  package net.javagarage.enums;
  /*
  File: EnumSwitch.java
  Purpose: show how to switch against the values in an enum.
  */
  public class EnumSwitch {
  private enum Color { red, blue, green }
  //list the values
  public static void main(String[] args) {
  //refer to the qualified value
  doIt(Color.red);
  }
  /*note that you switch against the UNQUALIFIED name. that is, "case Color.red:" is a
  compiler error */
  private static void doIt(Color c){
  switch (c) {
  case red:
  System.out.println("value is "   Color.red);
  break;
  case green:
  System.out.println("value is "   Color.green);
  break;
  case blue:
  System.out.println("value is : "   Color.blue);
  break;
  default :
  System.out.println("default");
  }
  }