Jafe Lee

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            49 隨筆 :: 0 文章 :: 24 評論 :: 0 Trackbacks
          枚舉類型其實是一個有限制的類,很多類的語法都可以用在枚舉上面上,例如自定義域、方法、構造方法等。先看下面一個例子:
          package?custom;
          public?enum?Prefix
          {
          ????
          //?These?are?the?values?of?this?enumerated?type.
          ????
          //?Each?one?is?followed?by?constructor?arguments?in?parentheses.
          ????
          //?The?values?are?separated?from?each?other?by?commas,?and?the
          ????
          //?list?of?values?is?terminated?with?a?semicolon?to?separate?it?from
          ????
          //?the?class?body?that?follows.
          ????MILLI("m",?.001),?
          ????CENTI(
          "c",?.01),?
          ????DECI(
          "d"),?
          ????DECA(
          "D",?10.0),?
          ????HECTA(
          "h",?100.0),?
          ????KILO(
          "k",?1000.0);??//?Note?semicolon

          ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
          //?semicolon

          ????
          //?This?is?the?constructor?invoked?for?each?value?above.
          ????private?Prefix(String?abbrev,?double?multiplier)
          ????{
          ????????
          this.abbrev?=?abbrev;
          ????????
          this.multiplier?=?multiplier;
          ????}
          ????
          ????
          //Another?constructor
          ????private?Prefix(String?abbrev)
          ????{
          ????????
          this.abbrev?=?abbrev;
          ????????
          this.multiplier?=?.1;
          ????}
          ????
          ????
          //?These?are?the?private?fields?set?by?the?constructor
          ????private?String?abbrev;
          ????
          private?double?multiplier;

          ????
          //?These?are?accessor?methods?for?the?fields.?They?are?instance?methods
          ????
          //?of?each?value?of?the?enumerated?type.
          ????public?String?abbrev()
          ????{
          ????????
          return?abbrev;
          ????}

          ????
          public?double?multiplier()
          ????{
          ????????
          return?multiplier;
          ????}
          }
          需注意,一個枚舉類型可以有多個構造器。

          posted on 2007-09-08 15:39 Jafe Lee 閱讀(1271) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 游戏| 车险| 宁明县| 大冶市| 交口县| 武陟县| 丹棱县| 固阳县| 瑞安市| 衢州市| 平泉县| 利津县| 盐城市| 桂东县| 石狮市| 霸州市| 临潭县| 六枝特区| 民乐县| 茌平县| 桐乡市| 舟曲县| 龙门县| 青州市| 新巴尔虎右旗| 吉木萨尔县| 延庆县| 惠来县| 翼城县| 汶川县| 宽城| 齐齐哈尔市| 苗栗县| 松溪县| 韶关市| 宣威市| 阆中市| 色达县| 广州市| 汝阳县| 怀宁县|