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
          主站蜘蛛池模板: 六盘水市| 简阳市| 女性| 嘉黎县| 白山市| 修武县| 扎鲁特旗| 十堰市| 民丰县| 安西县| 景泰县| 刚察县| 新晃| 四会市| 阿城市| 柯坪县| 广德县| 万全县| 腾冲县| 隆尧县| 钟山县| 柞水县| 读书| 增城市| 新宾| 南昌市| 恩平市| 西安市| 阿合奇县| 调兵山市| 上犹县| 囊谦县| 平凉市| 清水河县| 磴口县| 疏勒县| 罗江县| 十堰市| 牡丹江市| 兴和县| 奇台县|