Java筆記--枚舉&註解

來源:互聯網
上載者:User

標籤:

1、自訂枚舉類的實現,例:

class Season{    //1,提供類的屬性,聲明為rivate final    private final String name;    private final String Desc;    //2,聲明為final的屬性,在構造器中初始化,構造器為private    private Season(String name, String desc){        this.name = name;        this.Desc = desc;    }    //3,通過公用的方法來調用屬性    public String getName() {        return name;    }    public String getDesc() {        return Desc;    }    //4,建立對象,用public static final來修飾    public static final Season SPRING = new Season("spring", "春天");    public static final Season SUMMER = new Season("Summer", "夏天");    public static final Season AUTUMN = new Season("Autumn", "秋天");    public static final Season WINTER = new Season("winter", "冬天");        @Override    public String toString() {        return "Season [name=" + name + ", Desc=" + Desc + "]";    }    }

2、使用enum關鍵字定義枚舉類:

public enum Season {    SPRING("spring", "春天"),    SUMMER("Summer", "夏天"),    AUTUMN("Autumn", "秋天"),    WINTER("winter", "冬天");    private final String name;    private final String desc;        public String getName() {        return name;    }    public String getDesc() {        return desc;    }        private Season(String name, String desc){        this.name = name;        this.desc = desc;    }}

3、枚舉類常用方法:

  1)values();

  2)vauueOf(String name);

//1.values()Season[] seasons = Season.values();for(int i = 0; i < seasons.length; i++){    System.out.println(seasons[i]);}//2.valueOf(String name):傳入的參數必須是枚舉類存在的對象名字//否則報java.lang.IllegalArgumentException的異常String str = "SPRING";Season season = Season.valueOf(str);System.out.println(season);

 4、枚舉類也可以實現介面,可以讓不同的枚舉類對象調用被重寫的抽象方法,執行效果不同(讓每個對象分別重寫方法)。

5、元素據(MetaData),就是註解(Annotation),是代碼裡的特殊標記,這些標記可以在編譯,類載入,運行時被讀取,並執行相應的處理,通過使用Annotation,程式員可以在不改變原有邏輯的情況下,在源檔案中嵌入一些補充資訊。

6、Annotation可以像修飾符一樣被使用,可用於修飾包、類、構造器、方法、成員變數、參數、局部變數的聲明、這些資訊被儲存在Annotation的“name=value”對中。

7、Annotation能被用來為程式元素設定中繼資料。

8、JDK中的3個基本註解類型:

  1)@Override:限定重寫父類方法。該注釋只能用於方法;

  2)@Deprecated:用於表示某個程式元素(類、方法等)已淘汰;

  3)@SuppressWarnings:抑制編譯器警告。

9、自訂Annotation:

public @interface MyAnnotation {    String value() default "MyAnnotation";}

10、JDK的元Annotation用於修飾其他Annotation定義,JDK5.0提供了專門在註解上的註解類型,分別是:

  1)Retention:只能用於修飾一個Annotation定義,用於指定該Annotation可以保留多長時間。@Rentention包含一個RetentionPolicy類型的成員變數,使用                                @Rentention時必須為該value成員變數指定值(RetentionPolicy.SOURCE、RetentionPolicy.CLASS、RetentionPolicy.RUNTIME)

  2)Target:用於修飾Annotation定義,用於指定被修飾的Annotation能用於修飾哪些程式元素,@Target也包含一個名為value的成員變數。

  3)Documented:用於指定被該元Annotation修飾的Annotation類將被javadoc工具提取成文檔(定義為Documented的註解必須設定Retention值為RUNTIME)。

  4)Inherited:被它修飾的Annotation將具有繼承性,如果某個類使用了被@Inherited修飾的Annotation,則其子類將自動具有該註解(使用較少)。

 

Java筆記--枚舉&註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.