java enum 枚舉類

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

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

          public?class?SystemConstant?{
          ????
          /**
          ?????*?金庫(kù) sourceortarget?系統(tǒng)相關(guān)
          ?????
          */

          ????
          public?static?final?String?CASHWASTEBOOK_SOURCEORTARGET_SYS?=?"系統(tǒng)";
          ????
          /**
          ?????*?附件上傳路徑
          ?????
          */
          ?
          ????
          public?static?String?UPLOAD_ATTACHMENT_DIR="upload\\";
          ????
          public?static?String?CONFIG_DIR="config\\";
          ????
          /**
          ?????*?臨時(shí)文件路徑
          ?????
          */

          ????
          public?static?String?TEMP_DIR="temp\\";
          ????
          /**
          ?????*?會(huì)員關(guān)系
          ?????
          */

          ????
          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?=?"您不能選擇管理員登錄";
          ????
          /**管理員選擇會(huì)員或家長(zhǎng)登錄*/
          ????
          public?static?final?String?MESSAGE_LOGIN_TYPEERROR2?=?"您應(yīng)該選擇管理員登錄";
          ????
          /**會(huì)員或家長(zhǎng)重復(fù)登陸*/
          ????
          public?static?final?String?MESSAGE_LOGIN_REPEAT?=?"可能因?yàn)橐韵略颍鸁o(wú)法登陸系統(tǒng)\n\t1 有人盜用您的帳號(hào)\n2?您的{0}正在使用本帳號(hào)";
          ????
          public?static?final?String?MESSAGE_LONGIN_PASSWORDERROR?=?"用戶名或密碼無(wú)效";
          ????
          public?static?final?String?MESSAGE_INSUFFICIENT_FUNDS?=?"您的帳戶余額不足";
          ????
          public?static?final?String?MESSAGE_MEMBER_ONLINETIME_FULL?=?"您今日的累計(jì)上線時(shí)間已超過(guò)1.5小時(shí)";
          ????
          /**會(huì)員每天最大登錄時(shí)限?單位分鐘?默認(rèn)90**/
          ????
          public?static?final?int?MEMBER_MAX_DAY_ONLINE_MINUTES?=?90;
          ????
          }

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

          ? 2 說(shuō)到從xml文件讀取屬性來(lái)動(dòng)態(tài)配置枚舉類,我下面就舉個(gè)例子,演示演示
          ???? 一些web系統(tǒng)中涉及到文件上傳,根據(jù)文件類型顯示相應(yīng)圖標(biāo),并且有些jsp,asp等等的文件不允許上傳,下面就是一個(gè)滿足這種需求的枚舉類,它最大的特點(diǎn)就是可以從xml中讀取配置信息
          /**
          ?*?系統(tǒng)中用到的文件擴(kuò)展名?枚舉類
          ?*?
          @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目錄中l(wèi)oad
          ?????*?
          ?????
          */

          ????
          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>

          可能系統(tǒng)中其他的一些枚舉類(比如1 中提到的RelationShip)也會(huì)用到非常類似的做法,這時(shí)候我們就可以重構(gòu)了,將一些共同的特點(diǎn)抽取到一個(gè)抽象類中。這將會(huì)在以后的文章中提到。
          有不同的觀點(diǎn),請(qǐng)聯(lián)系come2u at gmail.com? ,歡迎交流。

          posted on 2006-08-07 09:38 nbt 閱讀(658) 評(píng)論(0)  編輯  收藏 所屬分類: Java2SE相關(guān)

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          Java技術(shù)網(wǎng)站

          友情鏈接

          國(guó)內(nèi)一些開(kāi)源網(wǎng)站

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 富源县| 兴山县| 桂东县| 台安县| 新郑市| 交口县| 措美县| 平江县| 吉木萨尔县| 迭部县| 原阳县| 越西县| 仁怀市| 新疆| 界首市| 柳州市| 德钦县| 万山特区| 陇川县| 仁布县| 沾益县| 清新县| 新田县| 醴陵市| 武胜县| 大荔县| 泰和县| 姜堰市| 密云县| 龙门县| 会东县| 遵化市| 九龙城区| 淮滨县| 怀来县| 巴中市| 乌鲁木齐市| 永州市| 刚察县| 黑河市| 大方县|