<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 64,  comments - 9,  trackbacks - 0

    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

    package net.javagarage.enums;

     

    /*

    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");

          }

          }
    }

    為enums添加屬性和方法

    enums也可以象一般的類一樣添加方法和屬性,你可以為它添加靜態和非靜態的屬性或方法,這一切都象你在一般的類中做的那樣.

    package net.javagarage.enums;

     

    /*

    File: EnumDemo.java

    Purpose: show how to use an enum that also defines its own fields and methods

    */

     

    public class EnumWithMethods {

     

    //declare the enum and add values to it.

     

    public enum Season {

     

          winter, spring, summer, fall;

     

          private final static String location = "Phoenix";

     

          public static Season getBest(){

                if (location.equals("Phoenix"))

                      return winter;

                else

                      return summer;

     

          }

     

          public static void main(String[] args) {

     

          System.out.println(Season.getBest());

          }

    }

    就是這么的簡單.但是有一點是需要注意的,那就是enums的值列表必須緊跟在enum聲明,不然編譯時將會出錯.

    Enums構造函數:

    和類一樣enums也可以有自己的構造函數,如下:

    package net.javagarage.enums;

     

    public class EnumConstructor {

     

          public static void main(String[] a) {

     

                //call our enum using the values method

                for (Temp t : Temp.values())

                      System.out.println(t + " is : " + t.getValue());

          }

     

          //make the enum

          public enum Temp {

                absoluteZero(-459), freezing(32),

                boiling(212), paperBurns(451);

     

          //constructor here

          Temp(int value) {

                this.value = value;

          }

     

          //regular field?but make it final,

          //since that is the point, to make constants

          private final int value;

     

          //regular get method

          public int getValue() {

          return value;

          }

     

          }

    }
    輸出結果是:

    absoluteZero is : -459

    freezing is : 32

    boiling is : 212

    paperBurns is : 451

    盡管enums有這么多的屬性,但并不是用的越多越好,如果那樣還不如直接用類來的直接.enums的優勢在定義int最終變量僅當這些值有一定特殊含義時.但是如果你需要的是一個類,就定義一個類,而不是enum.

    0
    0
    (請您對文章做出評價)
    posted on 2009-10-19 12:05 super_nini 閱讀(269) 評論(0)  編輯  收藏

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


    網站導航:
     
    <2009年10月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 人与禽交免费网站视频| 一级一级毛片免费播放| 久久99国产乱子伦精品免费| 亚洲欧洲成人精品香蕉网| 久青草国产免费观看| 亚洲自偷自偷偷色无码中文| 一级毛片高清免费播放| 亚洲大尺度无码无码专区| 久久青草91免费观看| 亚洲国产成人综合| 一二三四在线观看免费高清中文在线观看| 亚洲激情校园春色| 精品久久免费视频| 国产成人亚洲精品91专区高清| 亚洲国产精品13p| 中文字幕乱码免费看电影| 亚洲AV永久无码区成人网站 | av免费不卡国产观看| 亚洲黄色免费电影| 成人a视频片在线观看免费| 国产成人综合亚洲一区| 亚洲午夜久久久久久久久电影网| 国产免费一区二区三区在线观看| 久久狠狠高潮亚洲精品| 在线观看免费a∨网站| 黄色一级免费网站| 亚洲成av人影院| 免费av欧美国产在钱| 全黄A免费一级毛片| 亚洲AV无码不卡在线播放| 国产精品免费精品自在线观看| 亚洲精品无码专区在线| 亚洲综合色在线观看亚洲| 青青青国产手机频在线免费观看| 77777午夜亚洲| 怡红院亚洲怡红院首页| 成人AV免费网址在线观看| 香港一级毛片免费看| 亚洲一区二区电影| 国产人成免费视频| 一级毛片免费观看不卡的|