table

          java enum type

          在像C這樣強(qiáng)調(diào)數(shù)據(jù)結(jié)構(gòu)的語言里,枚舉是必不可少的一種數(shù)據(jù)類型。然而在java的早期版本中,是沒有一種叫做enum的獨(dú)立數(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)付的過去??墒请S著java語言的發(fā)展,越來越多的呼聲要求把enum這種數(shù)據(jù)結(jié)構(gòu)獨(dú)立出來,加入到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  

          注意事項(xiàng)已經(jīng)在code中注釋出,還要說明一點(diǎn)的是,這個java文件編譯完成后不只有一個EnumDemo.class,還會有一個EnumDemo$Seasons.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: Seasons.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. }  

          Ok,so much for enum. Isn't it simple?

          posted on 2008-12-15 10:31 小卓 閱讀(222) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 山东| 嘉义县| 利津县| 哈巴河县| 平南县| 罗定市| 开江县| 句容市| 尖扎县| 新乡市| 吉木乃县| 伊川县| 新平| 襄垣县| 随州市| 宁南县| 石景山区| 筠连县| 日土县| 普洱| 明星| 大庆市| 临沂市| 乐业县| 聊城市| 牙克石市| 松江区| 宁国市| 江都市| 利川市| 桦南县| 九台市| 鱼台县| 大庆市| 林周县| 五寨县| 绿春县| 阳信县| 平利县| 修武县| 班戈县|