<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    一個枚舉類型的基類for j2se 1.4

    Posted on 2006-04-26 22:09 iceboundrock 閱讀(912) 評論(0)  編輯  收藏 所屬分類: J2SE隨筆
    ?1 import ?java.util.HashMap;
    ?2
    ?3 /**
    ?4 ?*?枚舉類型的基類
    ?5 ?*? @author ?Richard?Lee
    ?6 ?*
    ?7 ? */

    ?8 public ? abstract ? class ?EnumBase? {
    ?9
    10 ???? ////////////////////////////////////////// /
    11 ???? // 成員變量
    12 ???? private ? final ?String?name;
    13 ???? private ? int ?value? = ? - 1 ;
    14
    15 ????
    16 ???? ////////////////////////////////////////// /
    17 ???? // 類靜態變量
    18 ???? private ? static ? final ?HashMap?valueCollection? = ? new ?HashMap();
    19 ???? private ? static ? final ?HashMap?nameCollection? = ? new ?HashMap();
    20 ????
    21 ???? ////////////////////////////////////////// /
    22 ???? // 類靜態方法
    23 ???? public ? static ?EnumBase?valueOf(Class?enumType?,? int ?value) {
    24 ???????? return ?(EnumBase)valueCollection.get(getEnumValueKey(enumType,?value));
    25 ????}

    26 ????
    27 ???? public ? static ?EnumBase?parse(Class?enumType?,?String?s) {
    28 ???????? return ?(EnumBase)nameCollection.get(getEnumNameKey(enumType?,?s));
    29 ????}

    30 ????
    31 ???? private ? static ?String?getEnumValueKey(Class?enumClass,? int ?value)? {
    32 ???????? return ?enumClass.getName()? + ? " , " ? + ?value;
    33 ????}

    34
    35 ???? private ? static ?String?getEnumNameKey(Class?enumType?,?String?name) {
    36 ???????? return ?enumType? + ? " , " ? + ?name;
    37 ????}

    38 ????
    39 ???? ////////////////////////////////////////// /
    40 ???? // 構造函數
    41 ???? protected ?EnumBase(String?name) {
    42 ????????checkName(name);
    43 ???????? this .name? = ?name;
    44 ????????nameCollection.put(getEnumNameKey(name)?,? this );
    45 ????}

    46 ????
    47 ???? protected ?EnumBase(String?name?,? int ?value) {
    48 ???????? this (name);
    49 ????????
    50 ????????checkValue(value);
    51 ???????? this .value? = ?value;
    52 ????????valueCollection.put(getEnumValueKey(value)?,? this );
    53 ????}

    54
    55 ????
    56 ???? ////////////////////////////////////////// /
    57 ???? // 成員函數
    58 ???? private ?String?getEnumNameKey(String?name)? {
    59 ???????? return ?getEnumNameKey( this .getClass()?,?name);
    60 ????}

    61
    62 ???? private ? void ?checkName(String?name)? {
    63 ???????? if ( null ? == ?name? || ? "" .equals(name)) {
    64 ???????????? throw ? new ?IllegalArgumentException( " Element?name?can?not?be?null?or?empty. " );
    65 ????????}

    66 ???????? if (nameCollection.containsKey(getEnumNameKey(name))) {
    67 ???????????? throw ? new ?IllegalStateException( " The?name?of?enum?element?already?exist. " );
    68 ????????}

    69 ????}

    70 ????
    71 ???? private ? void ?checkValue( int ?value)? {
    72 ???????? if (value? < ? 0 ) {
    73 ???????????? throw ? new ?IllegalArgumentException( " Element?value?must?not?less?than?0. " );
    74 ????????}

    75 ???????? if (valueCollection.containsKey(getEnumValueKey(value))) {
    76 ???????????? throw ? new ?IllegalStateException( " This?value?of?enum?element?already?exist. " );
    77 ????????}

    78 ????}

    79
    80 ???? private ?String?getEnumValueKey( int ?value)? {
    81 ???????? return ?getEnumValueKey( this .getClass()?,?value);
    82 ????}

    83
    84 ???? public ?String?toString()? {
    85 ???????? return ?name;
    86 ????}

    87 ????
    88 ???? public ? int ?getValue()? {
    89 ???????? if (value? > ? - 1 ) {
    90 ???????????? return ?value;
    91 ????????}
    else {
    92 ???????????? throw ? new ?IllegalStateException( " This?enum?class?has?no?value " );
    93 ????????}

    94 ????}

    95 }

    96

    使用范例:

    public ? class ?VaildEnum? extends ?EnumBase? {

    ?
    protected ?VaildEnum(String?name?,? int ?value)? {
    ??
    super (name?,?value);
    ?}

    ?
    ?
    public ? static ? final ?VaildEnum?Rule? = ? new ?VaildEnum( " Rule " ?,? 1 );
    ?
    public ? static ? final ?VaildEnum?OU? = ? new ?VaildEnum( " OU " ?,? 2 );
    }

    該枚舉基類對于j2sdk中常用的整型常量或者字符串型常量的優點:1.? 類型安全。2.性能提高(比字符串常量型)。3.在IDE中使用時會有提示,無需翻看SDK文檔。
    缺點:1.編寫略微麻煩。

    如果發現代碼中的bugs,歡迎您評論,非常感謝。


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


    網站導航:
     

    posts - 10, comments - 15, trackbacks - 0, articles - 0

    Copyright © iceboundrock

    主站蜘蛛池模板: 亚洲国产女人aaa毛片在线| 日韩精品电影一区亚洲| 成人无码视频97免费| 国产免费久久精品| 国产精品亚洲专区在线播放| 午夜dj免费在线观看| 亚洲欧美自偷自拍另类视| 成人毛片免费观看视频大全| 最新亚洲春色Av无码专区| 免费观看美女裸体网站| 狠狠综合亚洲综合亚洲色| 夜色阁亚洲一区二区三区| 国产精品福利在线观看免费不卡| 很黄很黄的网站免费的| 亚洲综合区图片小说区| 国产福利在线观看免费第一福利| 亚洲娇小性xxxx色| 日韩免费观看一区| 亚洲视频一区二区在线观看| 成人免费一级毛片在线播放视频| 亚洲字幕AV一区二区三区四区| 无码av免费一区二区三区| 亚洲精品在线播放| 在线观看免费成人| 一级成人生活片免费看| 国产成A人亚洲精V品无码| 免免费国产AAAAA片| 国产天堂亚洲国产碰碰| 国产精品亚洲аv无码播放| 男女猛烈激情xx00免费视频| 在线亚洲午夜理论AV大片| 日本高清免费中文在线看| 亚洲国产成人久久综合碰碰动漫3d| 免费观看黄色的网站| 黄网站色视频免费观看45分钟 | 久久久久亚洲国产AV麻豆 | 亚洲精品动漫在线| 日本黄色免费观看| 国产午夜免费高清久久影院| 国产AV无码专区亚洲AV漫画 | 久久天天躁狠狠躁夜夜免费观看|