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
          主站蜘蛛池模板: 贵州省| 绥德县| 雷波县| 安阳县| 娄底市| 翼城县| 万盛区| 大同市| 济阳县| 清丰县| 云南省| 临洮县| 定兴县| 本溪市| 舒兰市| 勃利县| 象山县| 庄河市| 楚雄市| 茂名市| 南安市| 朝阳区| 资源县| 石阡县| 鱼台县| 安庆市| 班戈县| 赤壁市| 女性| 会理县| 高阳县| 甘孜县| 潞城市| 新干县| 青河县| 买车| 南丰县| 长汀县| 弋阳县| 荆门市| 筠连县|