|
一、是什么java注釋? java sdk從jsee5開始添加了Annotation功能,其主要是為包、類 型、構造方法、方法、成員變量、參數(shù)、本地變量提供注釋功能。Annotation的添加對java代碼的正常執(zhí)行不會產(chǎn)生任何影響,只是起到一個注解的作用。Annotation定義的信息只能在程序運行時通過反射機制被訪問。 Annotation的使用場合,從我個人的應用范圍內(nèi)主要涉及到如下幾點: (1)可以標注一些變量的特殊狀態(tài),比如不能為空; (2)可以再導出或者顯示某些內(nèi)容時,只顯示或?qū)С鰩в心撤N特殊標注的字段; 使用中就用到了以上兩點,有使用過的朋友可以列出更多的使用的場合,共同學習。 二、java 標準的注釋有哪些? @Deprecated :被@Deprecated標注的對象class, method等被注明為不推薦使用(已過時)。使用被 Deprecated注釋的方法,編譯器會 提示方法過時警告(”Warring”),當程序仍然能正常運行。當某個方法已經(jīng)有新的替代方法或者不再推薦使用時,為了兼容以前程序的正常運行,可以再方法頭部加上該注解,以提示用戶該方法已過期。 @Override :注明對象method重載了父類的方法。一方面表示了該方法是重載父類的方法,另一方面用于檢查該方法的名稱拼寫是否正確。(僅用于方法) @SuppressWarnings:單一注釋,可以通過數(shù)組提供變量,變量值指明要阻止的特定類型警告(忽略某些警告)。數(shù)組中的變量指明要阻止的警告@SuppressWarnings(value={”unchecked”,”fallthrough”})) @Target :target說明了annotation所修飾的對象范圍:annotation可被用于packages、 types(類、接口、枚舉、annotation類型)、類型成員(方法、構造方法、成員變量、枚舉值)、方法參數(shù)和本地變量(如循環(huán)變量、catch 參數(shù))。在annotation類型的聲明中使用了target可更加明晰其修飾的目標。 @Retention :定義annotation的生命周期。 三、java標準注釋的使用 @Deprecated的使用 (1)包含被@Deprecated注釋的類 1 public class DeprecatedTest { 2 /** 3 * 過期注解的使用 4 * 定義如下: 5 * @Documented 6 * @Retention(RetentionPolicy.RUNTIME)// 7 * public @interface Deprecated { 8 * } 9 * 如果在某方法上加了@Deprecated注解,則該方法名稱會被劃上中劃線 10 */ 11 @Deprecated 12 public void iamOld(){ 13 System.out.println("我已經(jīng)過期了!"); 14 } 15 }
(2) 調(diào)用方法 1 public class StandardTest { 2 3 public static void main(String args[]){ 4 5 //帶有@Deprecated注解方法的調(diào)用(出現(xiàn)“The method iamOld() from the type DeprecatedTest is deprecated”提示) 6 new DeprecatedTest().iamOld(); 7 8 } 9 }
@ Override的使用 (1)父類 1 public class OverrideParent { 2 public void override(){ 3 System.out.println("我在子類中被重寫?。?); 4 } 5 }
(2)子類 1 public class OverrideTest extends OverrideParent { 2 3 public static void main(String[] args) { 4 new OverrideTest().override(); 5 } 6 @Override 7 public void override(){ 8 System.out.println("我重寫了父類的override方法!"); 9 } 10 11 }
該注解對于不同的版本的使用也是有點區(qū)別的,請參見:JAVA中 @Override 的作用(1.5和1.6的區(qū)別) @Target的使用 sdk中target的定義如下: 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType.ANNOTATION_TYPE) 4 public @interface Target { 5 ElementType[] value(); 6 }
從target的定義中我們可以看到,target中定義了一個ElementType枚舉類型的一個屬性,該屬性用于指示注釋所能使用的范圍,ElementType的定義如下: 1 public enum ElementType { 2 /** 用于類、接口(包含注解)、枚舉的定義 (Class, interface (including annotation type), or enum declaration) */ 3 TYPE, 4 5 /** 字段包括枚舉常量 (Field declaration (includes enum constants) )*/ 6 FIELD, 7 8 /** 方法的聲明(Method declaration) */ 9 METHOD, 10 11 /** 參數(shù)聲明(Parameter declaration) */ 12 PARAMETER, 13 14 /** 構造方法的聲明 (Constructor declaration) */ 15 CONSTRUCTOR, 16 17 /** 本地變量的聲明 (Local variable declaration) */ 18 LOCAL_VARIABLE, 19 20 /** 注解類型的聲明 (Annotation type declaration) */ 21 ANNOTATION_TYPE, 22 23 /** 包的聲明 ( Package declaration) */ 24 PACKAGE 25 }
該注解在定義其他注解時可以沒有,若沒有說明在使用時沒有任何限制。 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType.TYPE) 4 public @interface ClassAnno {}
(2)基于字段的注解 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType.FIELD) 4 public @interface MenuAnno {}
(3)基于方法的注解 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType. METHOD ) 4 public @interface MethodAnno {}
(4)基于參數(shù)聲明的注解 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType. PARAMETER ) 4 public @interface ParamAnno {}
(5)基于構造方法的注解 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType. CONSTRUCTOR ) 4 public @interface ConstructorAnno {}
(6) 基于本地變量的注解 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType. LOCAL_VARIABLE ) 4 public @interface LocalParamAnno {}
(7)基于注釋的注解 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType. ANNOTATION_TYPE ) 4 public @interface AnnoTypeAnno {}
(8) 基于包的注解 1 @Documented 2 @Retention(RetentionPolicy.RUNTIME) 3 @Target(ElementType. PACKAGE ) 4 public @interface PackageAnno {}
@Retention的使用 指示注釋類型的注釋要保留多久。如果注釋類型聲明中不存在 Retention 注釋,則保留策略默認為 RetentionPolicy.CLASS。 Retention的定義如下: 1 @Documented 2 @Retention(value=RUNTIME) 3 @Target(value=ANNOTATION_TYPE) 4 public @interface Retention{ 5 RetentionPolicy value(); 6 }
同樣Retention也只有一個枚舉類型的屬性,其定義如下: 1 public enum RetentionPolicy { 2 /** 3 * 編譯器要丟棄的注釋(只在源文件中存在)。 4 * Annotations are to be discarded by the compiler. 5 */ 6 SOURCE, 7 8 /** 9 * 編譯器將把注釋記錄在類文件中,但在運行時 VM 不需要保留注釋。這是默認值。 10 * Annotations are to be recorded in the class file by the compiler 11 * but need not be retained by the VM at run time. This is the default 12 * behavior. 13 */ 14 CLASS, 15 16 /** 17 * 編譯器將把注釋記錄在類文件中,在運行時 VM 將保留注釋,因此可以反射性地讀取。 18 * Annotations are to be recorded in the class file by the compiler and 19 * retained by the VM at run time, so they may be read reflectively. 20 * 21 * @see java.lang.reflect.AnnotatedElement 22 */ 23 RUNTIME 24 }
注意:只有將 RetentionPolicy 設置為class,在反射時才能獲取到給指定注解所設定的值! @Documented:是一個標記注釋,表示注釋應該出現(xiàn)在類的javadoc中,因為在默認情況下注釋時不包括在javadoc中的。 所以如果花費了大量的時間定義一個注釋類型,并想描述注釋類型的作用,可以使用它。 注意他與@Retention(RetentionPolicy.RUNTIME)配合使用,因為只有將注釋保留在編譯后的類文件中由虛擬機加載, 然后javadoc才能將其抽取出來添加至javadoc中。 參考網(wǎng)址: |
|
|