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 閱讀(1261) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 台中县| 濉溪县| 饶河县| 玉屏| 丹阳市| 鹤峰县| 遂溪县| 龙陵县| 青川县| 漳州市| 沙河市| 乐至县| 青阳县| 汉沽区| 习水县| 金乡县| 巨鹿县| 鹿泉市| 郓城县| 迭部县| 友谊县| 贺州市| 湾仔区| 本溪市| 长乐市| 贞丰县| 双流县| 陆良县| 旬阳县| 达日县| 闻喜县| 平罗县| 亚东县| 上思县| 合山市| 高淳县| 涿鹿县| 西和县| 汶上县| 双鸭山市| 镇赉县|