Jafe Lee

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            49 隨筆 :: 0 文章 :: 24 評(píng)論 :: 0 Trackbacks
          一個(gè)枚舉類(lèi)型是不能extends任何類(lèi)或另一個(gè)枚舉類(lèi)型的。但是可以實(shí)現(xiàn)一個(gè)或多個(gè)接口。例如
          package?implinterface;

          public?interface?Abbrevable
          {
          ????String?abbrev();
          }

          package?implinterface;

          public?interface?Multiplierable
          {
          ????
          double?multiplier();
          }

          package?implinterface;
          public?enum?Prefix?implements?Abbrevable,?Multiplierable
          {
          ????
          //?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;
          ????}
          }


          package?implinterface;
          public?class?TestPrefix
          {
          ????
          public?static?void?main(String[]?args)
          ????{
          ????????Prefix?p?
          =?Prefix.MILLI;
          ????????System.out.println(p.abbrev());
          ????????System.out.println(Prefix.
          class);
          ????}
          }


          posted on 2007-09-08 16:05 Jafe Lee 閱讀(417) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 长岭县| 平舆县| 揭阳市| 灵川县| 临西县| 南木林县| 山东省| 衡阳县| 永川市| 二手房| 满城县| 奎屯市| 陇南市| 平顺县| 宜良县| 徐闻县| 肥西县| 门源| 新津县| 临海市| 拜城县| 固始县| 广平县| 女性| 洛阳市| 离岛区| 兖州市| 中西区| 双峰县| 永宁县| 东城区| 大丰市| 桃江县| 元阳县| 伊宁县| 屯门区| 裕民县| 敦煌市| 都江堰市| 从化市| 临武县|