java 5 中的enum的深入學習心得

          The full enum syntax actually provides quite a bit more power and flexibility:
          
          • You can define your own fields, methods, and constructors for the enumerated type.

          • If you define one or more constructors, you can invoke a constructor for each enumerated value by following the value name with constructor arguments in parentheses.

          • Although an enum may not extend anything, it may implement one or more interfaces.

          • Most esoterically, individual enumerated values can have their own class bodies that override methods defined by the type.

          Rather than formally specifying the syntax for each of these advanced enum declarations, we'll demonstrate the syntax in the examples that follow.

           
          for example:
           
          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",     .1),
          DECA("D",   10.0),
          HECTA("h", 100.0),
          KILO("k", 1000.0);  // Note semicolon
          // This is the constructor invoked for each value above.
          Prefix(String abbrev, double multiplier) {
          this.abbrev = abbrev;
          this.multiplier = multiplier;
          }
          // 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-12-06 10:55 劉錚 閱讀(442) 評論(0)  編輯  收藏 所屬分類: JAVA General

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 葫芦岛市| 琼中| 垫江县| 青神县| 吉木乃县| 斗六市| 淮南市| 监利县| 蒙自县| 乐平市| 金湖县| 海原县| 舞阳县| 鹤山市| 易门县| 尼木县| 沾益县| 赞皇县| 怀安县| 南和县| 乌拉特中旗| 安泽县| 太仆寺旗| 建德市| 巴马| 桃源县| 沁水县| 波密县| 芜湖县| 紫云| 大悟县| 涞源县| 咸丰县| 五家渠市| 新兴县| 青铜峡市| 张家界市| 温泉县| 锡林郭勒盟| 永川市| 锦屏县|