Jafe Lee

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            49 隨筆 :: 0 文章 :: 24 評論 :: 0 Trackbacks
          一個枚舉類型是不能extends任何類或另一個枚舉類型的。但是可以實現一個或多個接口。例如
          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 閱讀(421) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 黄浦区| 禹城市| 阿拉善左旗| 容城县| 丰镇市| 灵川县| 昭通市| 台中市| 呼和浩特市| 会昌县| 辉县市| 泗阳县| 博野县| 包头市| 石柱| 美姑县| 项城市| 辽中县| 赫章县| 湘乡市| 岳西县| 毕节市| 讷河市| 阳东县| 航空| 开化县| 大方县| 沿河| 莱西市| 阿勒泰市| 永春县| 田林县| 麦盖提县| 南雄市| 忻城县| 宣威市| 澄江县| 炉霍县| 略阳县| 图木舒克市| 山西省|