幻境
          We are extremely fortunate not to know precisely the kind of world we live in
          posts - 22,comments - 39,trackbacks - 0

          枚舉

          在過去,我們必須用整型常數(shù)代替枚舉,隨著J2SE 5.0的發(fā)布,這樣的方法終于一去不復(fù)返了。

          一個簡單的枚舉類型定義如下:

          public enum Weather

          {

                 SUNNY,RAINY,CLOUDY

          }

           

          枚舉可以用在switch語句中:

          Weather weather=Weather.CLOUDY;

          switch(weather)

          {

                 case SUNNY:

                        System.out.println("It's sunny");

                        break;

                 case CLOUDY:

                        System.out.println("It's cloudy");

                        break;

                 case RAINY:

                        System.out.println("It's rainy");

                        break;

          }

           

          枚舉類型可以有自己的構(gòu)造方法,不過必須是私有的,也可以有其他方法的定義,如下面的代碼:

          public enum Weather {

              SUNNY("It is sunny"),

              RAINY("It is rainy"),

              CLOUDY("It is cloudy");

           

              private String description;

           

              private Weather(String description) {

                 this.description=description;

              }

             

              public String description() {

                 return this.description;

              }

          }

           

          下面一段代碼是對這個枚舉的一個使用:

          for(Weather w:Weather.values())

          {

              System.out.printf(                                                  "Description of %s is \"%s\".\n",w,w.description());

          }

           

          Weather weather=Weather.SUNNY;

          System.out.println(weather.description() + " today");

           

          如果我們有一個枚舉類型,表示四則運算,我們希望在其中定義一個方法,針對不同的值做不同的運算,那么我們可以這樣定義:

          public enum Operation {

               PLUS, MINUS, TIMES, DIVIDE;

           

                  // Do arithmetic op represented by this constant

                  double eval(double x, double y){

                      switch(this) {

                          case PLUS:   return x + y;

                          case MINUS:  return x - y;

                          case TIMES:  return x * y;

                          case DIVIDE: return x / y;

                      }

                      throw new AssertionError("Unknown op: " + this);

                  }

          }

           

          這樣寫的問題是你如果沒有最后一行拋出異常的語句,編譯就無法通過。而且如果我們想要添加一個新的運算,就必須時刻記著要在eval中添加對應(yīng)的操作,萬一忘記的話就會拋出異常。

          J2SE 5.0提供了解決這個問題的辦法,就是你可以把eval函數(shù)聲明為abstract,然后為每個值寫不同的實現(xiàn),如下所示:

           

          public enum Operation {

               PLUS   { double eval(double x, double y) { return x + y; } },

               MINUS  { double eval(double x, double y) { return x - y; } },

               TIMES  { double eval(double x, double y) { return x * y; } },

               DIVIDE { double eval(double x, double y) { return x / y; } };

           

               abstract double eval(double x, double y);

          }

           

          這樣就避免了上面所說的兩個問題,不過代碼量增加了一些,但是隨著今后各種Java開發(fā) IDE的改進(jìn),代碼量的問題應(yīng)該會被淡化。

          posted on 2005-05-12 10:57 閱讀(1200) 評論(0)  編輯  收藏 所屬分類: 編程相關(guān)
          主站蜘蛛池模板: 黎平县| 南城县| 甘肃省| 迁安市| 彭州市| 兴化市| 永福县| 定远县| 兴文县| 六枝特区| 宝应县| 怀化市| 光泽县| 南乐县| 德安县| 辽阳县| 克什克腾旗| 余干县| 任丘市| 黎城县| 嘉黎县| 双桥区| 东台市| 青龙| 昌图县| 洛隆县| 长海县| 东兴市| 黔南| 宁津县| 吴川市| 彩票| 嵊州市| 南澳县| 陇川县| 大理市| 台中县| 崇信县| 清水县| 阿克苏市| 博乐市|