java中的enum

          Enum是enumeration(列舉)的簡寫形式,包含在java.lang包中.熟悉C, C++, C#, 或 Pascal人應該對列舉有所了解,先看個例子:

           public enum Season { winter, spring, summer, fall }


          一個enum是定義一組值的對象,它可以包括零個或多個值成員.它是屬于enum類型的,一個enum對象中不可有兩個或多個相同的屬性或值.在次之前的java程序員一般是 用接口的方法實現列舉的,如 :

           

          public interface Season {
              
          static winter = 0;
              
          static spring = 1//etc..
              }

          引入了enum的java的列舉的編寫方便了許多,只須定義一個enum型的對象.enum對象的值都回自動獲得一個數字值,從0開始,依次遞增.看一個比較簡單的enum實現的例子:EnumDemo.java

              /*
              We can loop over the values we put into the enum
              using the values() method.
              Note that the enum Seasons is compiled into a
              separate unit, called EnumDemo$Seasons.class
              
          */

              
          public class EnumDemo {
              
          /*declare the enum and add values to it. note that, like in C#, we don't use a ; to
              end this statement and we use commas to separate the values 
          */

              
          private enum Seasons { winter, spring,
              summer, fall }

              
          //list the values
              public static void main(String[] args) {
              
          for (Seasons s : Seasons.values()){
              System.out.println(s);
              }

              }

              }

          運行上述代碼你回得到 以下結果:

              winter
              spring
              summer
              fall
              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");
              }

              }

              }

          posted on 2008-12-20 20:03 丁克設計 閱讀(748) 評論(0)  編輯  收藏 所屬分類: JAVA技術文檔


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          留言簿(6)

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 义乌市| 云梦县| 普洱| 惠东县| 正镶白旗| 上犹县| 中宁县| 莒南县| 措勤县| 昌都县| 新民市| 临城县| 呼图壁县| 甘肃省| 城口县| 崇阳县| 黄山市| 朝阳区| 和平县| 肇东市| 凉山| 崇义县| 沾益县| 阿拉善右旗| 和硕县| 武隆县| 阿勒泰市| 淳安县| 昭苏县| 神池县| 廊坊市| 柳林县| 长子县| 大庆市| 六安市| 阜阳市| 甘南县| 开鲁县| 楚雄市| 齐河县| 根河市|