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


public?class?SystemConstant?
{

????/**?*//**
?????*?金庫 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\\";

????/**?*//**
?????*?臨時文件路徑
?????*/
????public?static?String?TEMP_DIR="temp\\";

????/**?*//**
?????*?會員關(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?=?"您不能選擇管理員登錄";

????/**?*//**管理員選擇會員或家長登錄*/
????public?static?final?String?MESSAGE_LOGIN_TYPEERROR2?=?"您應(yīng)該選擇管理員登錄";

????/**?*//**會員或家長重復(fù)登陸*/
????public?static?final?String?MESSAGE_LOGIN_REPEAT?=?"可能因為以下原因,您無法登陸系統(tǒng)\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小時";

????/**?*//**會員每天最大登錄時限?單位分鐘?默認(rèn)90**/
????public?static?final?int?MEMBER_MAX_DAY_ONLINE_MINUTES?=?90;
????
}

??
??? 可以看到,枚舉類型Relationship是定義一些會員關(guān)系之間的東東,其實我可以把它單獨定義一個類,或者放到Member(會員)這個類中,但綜合考慮,我還是覺得放到SystemConstant比較好,并且今后重構(gòu)SystemConstant,會添加從xml文件讀取屬性的功能。
?? ? 雖然Relationship是一個內(nèi)部類,但由于是靜態(tài)的,所以可以直接import,而無須每次都用SystemConstant.Relationship;
?例如:
?? ?public Relationship getRelationship() {
????????? ?return Relationship.valueOf(relationship);
?????? ?}
? 2 說到從xml文件讀取屬性來動態(tài)配置枚舉類,我下面就舉個例子,演示演示
???? 一些web系統(tǒng)中涉及到文件上傳,根據(jù)文件類型顯示相應(yīng)圖標(biāo),并且有些jsp,asp等等的文件不允許上傳,下面就是一個滿足這種需求的枚舉類,它最大的特點就是可以從xml中讀取配置信息

/**?*//**
?*?系統(tǒng)中用到的文件擴展名?枚舉類
?*?@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)也會用到非常類似的做法,這時候我們就可以重構(gòu)了,將一些共同的特點抽取到一個抽象類中。這將會在以后的文章中提到。
有不同的觀點,請聯(lián)系
come2u at gmail.com? ,歡迎交流。