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

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

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

    風(fēng)人園

    弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
    隨筆 - 99, 文章 - 181, 評(píng)論 - 56, 引用 - 0
    數(shù)據(jù)加載中……

    jdk1.5 Type Safe Enum

    Type Safe Enum

    Tiger的改進(jìn)其實(shí)真的稱得上是順應(yīng)民意,里面的好多特性都可以在兩本Java巨著<<Think In Java>;>;和<<Effective Java>;>;發(fā)現(xiàn)一些端倪.
    (不得不承認(rèn),<<Think In Java>;>;和<<Effective Java>;>;是Java歷史上最經(jīng)典的兩本書,多讀多受益)
    比如,之前提到過的Genric,在<<Think In Java>;>; 第九章介紹Colletion時(shí)就對(duì)Genric作出了預(yù)期,同時(shí)也實(shí)現(xiàn)某種形式的Genric(TIJ 2ND,P449-P455).
    另外,Think In Java里也提及了利用interface來定義一組常量的方法(P358-P360),不少標(biāo)準(zhǔn)的Java庫類也使用了這種方法(如java.util.zip.ZipConsts.不過,你將會(huì)發(fā)現(xiàn),在Tiger里它已經(jīng)不復(fù)存在了)
    這種做法叫做Const Interface,大家也許或多或少都使用過.但是,Const Interface的方式在<<Effective Java>;>;受到了極大的否定(Item 17).書里提出了使用Type Safe Enum的方法(Item 21)來取代Const Interface.讓我們對(duì)比一下以下兩段代碼

    Const Interface
    [code]
    //SeasonConst.java
    // Constant interface pattern - do not use!
    public interface SeasonConst {
    public static final int SPRING = 1;
    public static final int SUMMER = 2;
    public static final int AUTUMN = 3;
    public static final int WINTER = 4;
    }

    //TestConst.java
    //using of Constant interface
    public class TestConst implements SeasonConst{
    public void seasonLiving(int season){
    if(season==SPRING){
    System.out.println("we BBQ at SPRING");
    }
    else if(season==SUMMER){
    System.out.println("we swim at SUMMER");
    }
    else if(season==AUTUMN){
    System.out.println("we kite at AUTUMN");
    }
    else if(season==WINTER){
    System.out.println("we skate at WINTER");
    }
    else{
    System.out.println("What ?");

    }
    }
    public static void main(String args[]){
    TestConst t = new TestConst();
    t.seasonLiving(SPRING);
    t.seasonLiving(WINTER);
    t.seasonLiving(WINTER+1);
    //t.sersonLiving(WEEK_DAY.MONDAY);//程序無法和其他常數(shù)完整的區(qū)分
    }
    }
    [/code]

    Type Safe Enum
    [code]
    //SeasonEnum.java
    // The typesafe enum (j2sdk1.4)pattern
    public class SeasonEnum {
    private final String name;
    private SeasonEnum(String name) {
    this.name = name;
    }
    public String toString() { return name; }
    public static final SeasonEnum SPRING = new SeasonEnum("SPRING");
    public static final SeasonEnum SUMMER = new SeasonEnum("SUMMER");
    public static final SeasonEnum AUTUMN = new SeasonEnum("AUTUMN");
    public static final SeasonEnum WINTER = new SeasonEnum("WINTER");
    }

    //TestEnum.java
    //using of typesafe enum (j2sdk1.4)
    public class TestEnum{
    public void seasonLiving(SeasonEnum season){
    if(season==SeasonEnum.SPRING){
    System.out.println("we BBQ at "+season);
    }
    else if(season==SeasonEnum.SUMMER){
    System.out.println("we swim at "+season);
    }
    else if(season==SeasonEnum.AUTUMN){
    System.out.println("we kite at "+season);
    }
    else if(season==SeasonEnum.WINTER){
    System.out.println("we skate at "+season);
    }
    else{
    System.out.println("What ?");
    }
    }
    public static void main(String args[]){
    TestEnum t = new TestEnum();
    t.seasonLiving(SeasonEnum.SPRING);
    t.seasonLiving(SeasonEnum.WINTER);
    //t.seasonLiving(WINTER+1);no way to put a season not in the SeasonEnum

    }
    }
    [/code]

    可以看出,Type Safe Enum在以下幾個(gè)方面優(yōu)于Const Interface
    1.不會(huì)違背OO繼承的意義.
    2.類型安全,不會(huì)有無意義的常數(shù)混進(jìn)正常常數(shù)中
    3.保持自己的獨(dú)立性,不像Const interface再編譯后就失去了意義
    4.可直接用于print
    5.作為一個(gè)Object,可以擁有自己的方法,變量,可以繼承,implement interface

    但是,相對(duì)的,Type Safe Enum在使用中也有幾點(diǎn)不利的地方

    1.不能直接用于switch
    2.必須跟上類名,增加復(fù)雜性
    3.繁瑣

    于是,Tiger中引入了 typesafe Enum和static import ,同時(shí)啟用了一個(gè)新的關(guān)鍵字enum(這也是Tiger中引入的唯一一個(gè)關(guān)鍵字).再配合上Genric,使得整個(gè)結(jié)構(gòu)變得嚴(yán)謹(jǐn)而易用.

    讓我們還是使用方才的例子來說明吧,先來一個(gè)最簡單的例子.
    [code]
    public class TestEnumTiger1{
    public enum SeasonSimpliest {? ? ? ? SPRING,? ? ? ? SUMMER,? ? ? ? AUTUMN,? ? ? ? WINTER}


    public void seasonLiving(SeasonSimpliest season){

    if(season==SeasonSimpliest.SPRING){
    System.out.println("we BBQ at "+season);
    }
    else if(season==SeasonSimpliest.SUMMER){
    System.out.println("we swim at "+season);
    }
    else if(season==SeasonSimpliest.AUTUMN){
    System.out.println("we kite at "+season);
    }
    else if(season==SeasonSimpliest.WINTER){
    System.out.println("we skate at "+season);
    }
    else{
    System.out.println("What ?");
    }
    }
    public static void main(String args[]){
    TestEnumTiger1 t = new TestEnumTiger1();
    t.seasonLiving(SeasonSimpliest.SPRING);
    t.seasonLiving(SeasonSimpliest.WINTER);
    System.out.println("the seasong is :"+SeasonSimpliest.SPRING);
    //t.seasonLiving(WINTER+1);no way to put a season not in the SeasonEnum
    //t.seasonLiving(new SeasonSimpliest("Five Season"));no way to put a season not in the SeasonEnum

    }
    }[/code]

    這里面的常量直接用enum修飾,再加上一個(gè)名字和所需常量就可以了.
    enum SeasonSimpliest {? ? ? ? SPRING,? ? ? ? SUMMER,? ? ? ? AUTUMN,? ? ? ? WINTER}
    不過,不要被這種形式所迷惑,以為enum是一個(gè)primtive type,與int,boolean一樣,那就錯(cuò)了.
    讓我們編譯一下這個(gè)文件,再看看編譯后的目錄
    里面除了有TestEnumTiger1.java,TestEnumTiger1.class之外,還有一個(gè)TestEnumTiger1$SeasonSimpliest.class,這就暴露了enum的某些行為方式,它在某種意義上是一個(gè)class.
    在上面的源碼中,它成了一個(gè)inner-class.

    OK.讓我們改改代碼,把enum作為一個(gè)獨(dú)立個(gè)體來處理
    [code]
    //SeasonSimpliest.java
    package test;
    public enum SeasonSimpliest {SPRING,SUMMER,AUTUMN,WINTER}
    [/code]

    [code]
    //enum & static import
    package test;
    import static test.SeasonSimpliest.*;

    public class TestEnumTiger2{

    public void seasonLiving(SeasonSimpliest season){
    switch(season){
    case SPRING:
    System.out.println("we BBQ at "+season);
    break;
    case SUMMER:
    System.out.println("we swim at "+season);
    break;
    case AUTUMN:
    System.out.println("we kite at "+season);
    break;
    case WINTER:
    System.out.println("we skate at "+season);
    break;
    default:throw new AssertionError("something must be wrong,no such a season");
    }
    }
    public static void main(String args[]){
    TestEnumTiger2 t = new TestEnumTiger2();
    t.seasonLiving(SPRING);
    t.seasonLiving(WINTER);
    System.out.println("the season is :"+SUMMER);
    //t.seasonLiving(WINTER+1);no way to put a season not in the SeasonEnum
    //t.seasonLiving(new SeasonSimpliest("Five Season"));no way to put a season not in the SeasonEnum

    }
    }

    [/code]

    為了使整個(gè)代碼顯得更加簡潔,我引入import static ,稍后再作更詳細(xì)介紹,現(xiàn)在先來看看enum的好處.
    1.避免了使用Const interface
    2.很簡單的定義方式
    3.Type Safe,類型安全的,不會(huì)和其他的常量混繞.
    4.可以使用switch
    5.作為一個(gè)Class,它可以有自己Method,Field,也可以implements interface.

    為了更好的說明第5點(diǎn),我給大家展示一個(gè)復(fù)雜的enum type.

    [code]
    //SeasonComplex.java
    //implements interface,owner method & field
    public??enum SeasonComplex implements Comparable<SeasonComplex>; {
    ? ? ? ? SPRING(1),
    ? ? ? ? SUMMER(2),
    ? ? ? ? AUTUMN(3),
    ? ? ? ? WINTER(4);
    private final int seasonNumber;
    Season(int value){
    ? ? ? ? seasonNumber=value;
    ? ? ? ? }
    public int seasonNumber(){
    ? ? ? ? return seasonNumber;
    }
    public int compare(Season c){
    return seasonNumber - c.seasonNumber;
    }
    public String description(){
    return this+":第"+ seasonNumber +"季";
    }
    };

    [/code]

    具體的我就不一一寫清楚,大家好好體會(huì)吧.

    posted on 2006-12-16 15:50 風(fēng)人園 閱讀(562) 評(píng)論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 成人免费一区二区三区在线观看| 色多多A级毛片免费看| 久久免费观看国产99精品| 伊人久久大香线蕉亚洲| 色多多www视频在线观看免费| 免费A级毛片无码A∨男男| 特级无码毛片免费视频| 四虎成人精品在永久免费| 青娱乐在线视频免费观看| 亚洲国产精品专区在线观看| 色多多免费视频观看区一区| 亚洲人成网站色在线入口 | 亚洲午夜精品一区二区公牛电影院| a级成人毛片免费视频高清| 国产∨亚洲V天堂无码久久久| 91福利免费网站在线观看| 久久久亚洲欧洲日产国码农村| 久久免费福利视频| 亚洲男人天堂2018av| 亚洲精品自拍视频| 男女免费观看在线爽爽爽视频 | 亚洲成A人片在线观看无码3D| 国产高清对白在线观看免费91| 日本红怡院亚洲红怡院最新| 最近中文字幕国语免费完整| 亚洲人片在线观看天堂无码| 亚洲福利精品电影在线观看| 国产白丝无码免费视频| 亚洲午夜国产精品无卡| 国产精品久免费的黄网站| 中文在线观看国语高清免费| 亚洲成人福利网站| 免费一级e一片在线播放| a在线观看免费视频| 国产亚洲国产bv网站在线| 亚洲精品和日本精品| 最近2019中文字幕免费直播| 色天使亚洲综合一区二区| 亚洲大片在线观看| 波多野结衣免费视频观看| 麻豆视频免费观看|