JDK5.0新特性系列---3.枚举类型

 

/**

 *enum关键字表示枚举类型,它的作用相当于类声明中的class关键字

 *枚举类型不能有public的构造方法

 *所有的枚举值都是publicstaticfinal,这些修饰符都是自动加上,无须程序员手动添加

 *枚举值之间用逗号","分开,最后一个枚举值后面接分号";"

 *每一个枚举值是一个枚举类型的实例

 *可以在枚举类型中定义非枚举值变量,这些变量可以使用任何修饰符

 *变量和方法的定义必须在枚举值后面定义

*/

public class EnumType {

       /**Person的枚举类型*/

       enum Person{

              CHINESE,    //中国人

              AMERICAN,   //美国人

              ENGLISH;    //英国人

       }

       public static void main(String[] args){

              System.out.println("Person枚举值的数目: " + Person.values().length);

              //遍历枚举类型中的所有值

              System.out.println("Person枚举值如下: ");

              Person[] ps = Person.values();   //values()方法返回Person类型的数组

              for (Person p : ps){

                     System.out.print(p + " ");   //toString()方法返回枚举值的名称,此处打印CHINESE AMERICAN ENGLISH

              }

              System.out.println();

              Person p = Person.CHINESE;

              //比较枚举值

              if(p == Person.CHINESE){

                     System.out.println("p' value equals Person.CHINESE");

              }

              //使用valueOf获得字符串描述的枚举值

              p = Person.valueOf("AMERICAN");

              //switch中使用枚举值

              //switch中可以放置的类型有byte,short,int,char,enum,注意没有long

               switch(p){

              case CHINESE:

                     System.out.println("p is Chinese");

                     break;

              case AMERICAN:

                     System.out.println("p is American");

                     break;

              case ENGLISH:

                     System.out.println("p is English");

                     break;

              }

              //获得枚举值在枚举类型中声明的顺序

              System.out.println("AMERICAN的序号: " + Person.AMERICAN.ordinal());

              System.out.println("CHINESE的序号: " + Person.CHINESE.ordinal());

              System.out.println("ENGLISH的序号: " + Person.ENGLISH.ordinal());

             

              //使用更复杂的枚举类型ComplexPerson

              ComplexPerson cp = ComplexPerson.CHINESE;

              //因为为CHINESE枚举值覆盖了toString()方法,所以调用的是CHINESEtoString方法

              System.out.println("cp.toString(): " + cp);

              cp = ComplexPerson.AMERICAN;

              //因为没有为AMERICAN枚举值覆盖toString方法,所以调用默认的toSting方法

              cp = ComplexPerson.OTHER;

              System.out.println("cp.getValue(): " + cp.getValue());

       }

       /**一个更复杂的枚举类型*/

       enum ComplexPerson{

              //枚举值

              //CHINESEvalue属性为"中国人"

              CHINESE("中国人"){

                      public String toString(){

                            return "这是个中国人";

                     }

              },

              AMERICAN("美国 "),

              ENGLISH("英国人"){

                     public String toString(){

                            return "这是个英国佬";

                     }

              },

              OTHER{

                     public String toString(){

                            return "这是个其它国家的人";

                     }

              };

              private String value = null//枚举类值的value属性,只能声明在枚举值的后面

              //默认的构造方法

              ComplexPerson(){

                     value = "其他人";

              }

              //带参数的构造方法

              ComplexPerson(String value){

                     this.value = value;

              }

              //获取value属性

               public String getValue(){

                     return this.value;

              }

       }

}

 

 

 

 

posted @ 2011-12-17 23:23  远哥  阅读(395)  评论(0编辑  收藏