學(xué)習(xí)筆記

          Simple is beautiful.

          導(dǎo)航

          <2007年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          統(tǒng)計

          公告

          ...

          常用鏈接

          留言簿(1)

          隨筆分類(2)

          隨筆檔案(56)

          Weblog

          搜索

          最新評論

          評論排行榜

          [z] JDK5.0中的enum

          在像C這樣強調(diào)數(shù)據(jù)結(jié)構(gòu)的語言里,枚舉是必不可少的一種數(shù)據(jù)類型。然而在java的早期版本中,是沒有一種叫做enum的獨立數(shù)據(jù)結(jié)構(gòu)的。所以在以前的java版本中,我們經(jīng)常使用interface來simulate一個enum。
          java 代碼
          1. public interface Color {   
          2.     static int RED   = 1;   
          3.     static int GREEN     = 2;   
          4.     static int BLUE = 3;   
          5. }  

          雖然這種simulation比較麻煩,但在以前也還應(yīng)付的過去。可是隨著java語言的發(fā)展,越來越多的呼聲要求把enum這種數(shù)據(jù)結(jié)構(gòu)獨立出來,加入到j(luò)ava中。所以從java 1.5以后,就有了enum,這也是這篇blog要學(xué)習(xí)的topic。

          學(xué)習(xí)的最好方式就是例子,先來一個:

          java 代碼
          1. public class EnumDemo {   
          2.     private enum Color {red, blue, green}           //there is not a ";"   
          3.        
          4.     public static void main(String[] args) {   
          5.         for(Color s : Color.values()) {   
          6.             //enum的values()返回一個數(shù)組,這里就是Seasons[]   
          7.              System.out.println(s);   
          8.          }   
          9.      }   
          10. }  
          console results
          1. red   
          2. blue   
          3. green  

          注意事項已經(jīng)在code中注釋出,還要說明一點的是,這個java文件編譯完成后不只有一個EnumDemo.class,還會有一個EnumDemo$Color.class,奇怪吧!

          Another e.g.

          java 代碼
          1. public class EnumDemo {   
          2.     private enum Color {red, blue, green}           //there is not a ";"   
          3.        
          4.     public static void main(String[] args) {   
          5.          Color s = Color.blue;   
          6.            
          7.         switch (s) {   
          8.         case red:                     //notice: Color.red will lead to compile error   
          9.              System.out.println("red case");   
          10.             break;   
          11.         case blue:   
          12.              System.out.println("blue case");   
          13.             break;   
          14.         case green:   
          15.              System.out.println("green case");   
          16.             break;   
          17.         default:   
          18.             break;   
          19.          }   
          20.      }   
          21. }  

          這個例子要說明的就是case的情況。

          就這么多嗎,當(dāng)然不是,我們的enum結(jié)構(gòu)還可以定義自己的方法和屬性。

          java 代碼
          1. public class EnumDemo {   
          2.     private enum Color {   
          3.          red, blue, green;                          //there is a ";"   
          4.            
          5.         //notic: enum's method should be "static"   
          6.         public static Color getColor(String s){   
          7.             if(s.equals("red flag")){   
          8.                 return red;   
          9.              } else if(s.equals("blue flag")){   
          10.                 return blue;   
          11.              } else {   
          12.                 return green;   
          13.              }   
          14.          }   
          15.      }          //there is not ";"   
          16.        
          17.     public static void main(String[] args) {   
          18.          EnumDemo demo = new EnumDemo();   
          19.          System.out.println(demo.getFlagColor("red flag"));   
          20.      }   
          21.        
          22.     public Color getFlagColor(String string){   
          23.         return Color.getColor(string);   
          24.      }   
          25. }  

          posted on 2007-11-10 16:13 Ecko 閱讀(482) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 阜宁县| 尼木县| 北安市| 高唐县| 吴江市| 福贡县| 连山| 金坛市| 丹巴县| 阿拉善右旗| 蒲城县| 中方县| 大英县| 纳雍县| 禄丰县| 清新县| 綦江县| 巴中市| 利津县| 呼玛县| 丰台区| 壤塘县| 汤原县| 石泉县| 常宁市| 宁城县| 洪雅县| 宁安市| 祥云县| 图们市| 迁西县| 北碚区| 承德市| 长沙市| 迭部县| 呼图壁县| 监利县| 太谷县| 通河县| 南安市| 伊川县|