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
          主站蜘蛛池模板: 楚雄市| 腾冲县| 河池市| 洱源县| 平度市| 北海市| 墨竹工卡县| 拉萨市| 孟连| 灵武市| 曲周县| 南宫市| 体育| 墨玉县| 柳州市| 涡阳县| 庄河市| 疏附县| 铜梁县| 东源县| 嘉荫县| 沙湾县| 奉化市| 泸定县| 鹤岗市| 来凤县| 永寿县| 阳新县| 巴中市| 昌平区| 永康市| 浏阳市| 建水县| 秀山| 社会| 阳高县| 井陉县| 丰原市| 班戈县| 永平县| 松潘县|