java enum 枚舉類

          (轉自http://www.aygfsteel.com/dazuiba/archive/2006/08/04/j2se_enum.html)

          1 定義在常量類中
          ????
          ??? 經常碰到要將枚舉類當成常量使用的情況,這不僅可以將相關的常量定義到一個枚舉類中,而且還可以利用枚舉類強大而又靈活的功能,在加上編譯器內置的支持,使得在eclipse下的編程更方便,引入的bug更少。
          ??? 一般規模的項目中都會用一個單獨的類來定義系統中用到的常量,起碼筆者經歷的幾個項目都是有此種做法,該做法的好處就是便于集中管理,雖然這違背類封裝的原則,但鑒于其易用性,我們還是會常常這么做。
          ??? 例子:

          public?class?SystemConstant?{
          ????
          /**
          ?????*?金庫 sourceortarget?系統相關
          ?????
          */

          ????
          public?static?final?String?CASHWASTEBOOK_SOURCEORTARGET_SYS?=?"系統";
          ????
          /**
          ?????*?附件上傳路徑
          ?????
          */
          ?
          ????
          public?static?String?UPLOAD_ATTACHMENT_DIR="upload\\";
          ????
          public?static?String?CONFIG_DIR="config\\";
          ????
          /**
          ?????*?臨時文件路徑
          ?????
          */

          ????
          public?static?String?TEMP_DIR="temp\\";
          ????
          /**
          ?????*?會員關系
          ?????
          */

          ????
          public?static?enum?Relationship?{
          ????????GoodFriend(
          "親密好友"),
          ????????CommonFriend(
          "普通朋友"),
          ????????BLACK(
          "不受歡迎");
          ????????
          private???String?v;
          ????????
          ????????Relationship(String?value)?
          {
          ??????????v?
          =?value;
          ????????}

          ????????@Override
          ????????
          public?String?toString()?{????????
          ????????????
          return?v;
          ????????}
          ?
          ??????}
          ??
          ????
          public?static?final?String?SUCCESS?=?"OK";
          ????
          /**用戶選擇管理員登錄*/
          ????
          public?static?final?String?MESSAGE_LOGIN_TYPEERROR1?=?"您不能選擇管理員登錄";
          ????
          /**管理員選擇會員或家長登錄*/
          ????
          public?static?final?String?MESSAGE_LOGIN_TYPEERROR2?=?"您應該選擇管理員登錄";
          ????
          /**會員或家長重復登陸*/
          ????
          public?static?final?String?MESSAGE_LOGIN_REPEAT?=?"可能因為以下原因,您無法登陸系統\n\t1 有人盜用您的帳號\n2?您的{0}正在使用本帳號";
          ????
          public?static?final?String?MESSAGE_LONGIN_PASSWORDERROR?=?"用戶名或密碼無效";
          ????
          public?static?final?String?MESSAGE_INSUFFICIENT_FUNDS?=?"您的帳戶余額不足";
          ????
          public?static?final?String?MESSAGE_MEMBER_ONLINETIME_FULL?=?"您今日的累計上線時間已超過1.5小時";
          ????
          /**會員每天最大登錄時限?單位分鐘?默認90**/
          ????
          public?static?final?int?MEMBER_MAX_DAY_ONLINE_MINUTES?=?90;
          ????
          }

          ??
          ??? 可以看到,枚舉類型Relationship是定義一些會員關系之間的東東,其實我可以把它單獨定義一個類,或者放到Member(會員)這個類中,但綜合考慮,我還是覺得放到SystemConstant比較好,并且今后重構SystemConstant,會添加從xml文件讀取屬性的功能。
          ?? ? 雖然Relationship是一個內部類,但由于是靜態的,所以可以直接import,而無須每次都用SystemConstant.Relationship;
          ?例如:
          ?? ?public Relationship getRelationship() {
          ????????? ?return Relationship.valueOf(relationship);
          ?????? ?}

          ? 2 說到從xml文件讀取屬性來動態配置枚舉類,我下面就舉個例子,演示演示
          ???? 一些web系統中涉及到文件上傳,根據文件類型顯示相應圖標,并且有些jsp,asp等等的文件不允許上傳,下面就是一個滿足這種需求的枚舉類,它最大的特點就是可以從xml中讀取配置信息
          /**
          ?*?系統中用到的文件擴展名?枚舉類
          ?*?
          @author?zgy
          ?*
          ?
          */

          public?enum?FileExtension?{
          ????doc,?jsp,?jpeg,?jpg,?rar,?zip,?txt,unknown;

          ????
          private?boolean?allow;
          ?

          ????
          private?String?comment;

          ????
          private?String?iconPath;
          ????
          static?{
          ????????loadFromXml();?
          ????}
          ?
          ????FileExtension()?
          {
          ????????
          this.iconPath?=?"\\"?+?name();
          ????????
          this.allow?=?true;
          ????????
          this.comment?=?"comment?for"?+?name();
          ????}
          ????
          ????
          /**
          ?????*?從config目錄中load
          ?????*?
          ?????
          */

          ????
          private?static?void?loadFromXml()?{
          ????????
          try?{
          ????????????Document?doc?
          =?XmlUtil.parseXmlFile(SystemConstant.CONFIG_DIR
          ????????????????????
          +?"fileExtension.xml");
          ????????????NodeList?extensionList?
          =?doc.getElementsByTagName("FileExtension");
          ????????????
          for?(int?i?=?0;?i?<?extensionList.getLength();?i++)?{
          ????????????????Element?item?
          =?(Element)?extensionList.item(i);
          ????????????????String?name?
          =?item.getAttribute("name");
          ????????????????FileExtension?em?
          =?FileExtension.valueOf(name);
          ????????????????em.allow?
          =?Boolean.parseBoolean(item.getAttribute("allow"));
          ????????????????em.iconPath?
          =?item.getAttribute("iconPath");
          ????????????????em.comment?
          =?item.getAttribute("comment");?
          ????????????}

          ????????}
          ?catch?(Exception?e)?{?
          ????????????
          throw?new?RuntimeException(e);
          ????????}

          ????}


          ????
          public?boolean?isAllow()?{
          ????????
          return?allow;
          ????}


          ????
          public?String?getComment()?{
          ????????
          return?comment;
          ????}
          ?
          ????
          public?String?getUploadIcon()?{
          ????????
          return?iconPath;
          ????}


          ????
          public?static?void?main(String[]?args)?{
          ????????System.out.println(FileExtension.doc.comment);
          ????}

          }


          配置文件如下:config/fileExtension.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <FileExtensions>
          ?<FileExtension name="doc" iconPath="doc.jpg" allow="true"?? comment="文本"/>
          ?<FileExtension name="jpg" iconPath="jpg.jpg" allow="true"?? comment=""/>
          ?<FileExtension name="jpeg" iconPath="jpeg.jpg" allow="true" comment=""/>
          ?<FileExtension name="rar" iconPath="rar.jpg" allow="true"?? comment=""/>
          ?<FileExtension name="zip" iconPath="zip.jpg" allow="true"?? comment=""/>
          ?<FileExtension name="txt" iconPath="txt.jpg" allow="true"?? comment=""/>
          ??? <FileExtension name="jsp" iconPath="jsp.jpg" allow="false"? comment=""/>
          </FileExtensions>

          可能系統中其他的一些枚舉類(比如1 中提到的RelationShip)也會用到非常類似的做法,這時候我們就可以重構了,將一些共同的特點抽取到一個抽象類中。這將會在以后的文章中提到。
          有不同的觀點,請聯系come2u at gmail.com? ,歡迎交流。

          posted on 2006-08-07 09:38 nbt 閱讀(657) 評論(0)  編輯  收藏 所屬分類: Java2SE相關

          <2006年8月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          Java技術網站

          友情鏈接

          國內一些開源網站

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鸡西市| 孝感市| 岐山县| 晋江市| 庆城县| 东城区| 噶尔县| 逊克县| 普兰店市| 怀仁县| 肃南| 遵义县| 南宁市| 新丰县| 淮滨县| 禄劝| 出国| 加查县| 潍坊市| 永仁县| 洪泽县| 财经| 隆回县| 郎溪县| 固原市| 沁阳市| 泽普县| 女性| 上杭县| 得荣县| 固原市| 富平县| 门头沟区| 黎城县| 清河县| 屏东市| 巩留县| 沐川县| 伊川县| 曲沃县| 琼海市|