JAVA 註解樣本 詳解

來源:互聯網
上載者:User

轉載自:http://www.iteye.com/topic/400085

 

 註解(Annotation) 為我們在代碼中天界資訊提供了一種形式化的方法,是我們可以在稍後

某個時刻方便地使用這些資料(通過 解析註解 來使用這些資料)。

 

    註解的文法比較簡單,除了@符號的使用以外,它基本上與java的固有文法一致,java內建了三種

註解,定義在java.lang包中。

      @Override  表示當前方法是覆蓋父類的方法。

      @Deprecated  表示當前元素是不贊成使用的。

      @SuppressWarnings 表示關閉一些不當的編譯器警告資訊。

 

  下面是一個定義註解的執行個體

 

Java代碼
  1. package Test_annotation;   
  2.   
  3. import java.lang.annotation.Documented;   
  4. import java.lang.annotation.Inherited;   
  5. import java.lang.annotation.Retention;   
  6. import java.lang.annotation.Target;   
  7. import java.lang.annotation.ElementType;   
  8. import java.lang.annotation.RetentionPolicy;   
  9.   
  10. /*  
  11.  * 元註解@Target,@Retention,@Documented,@Inherited
     
  12.  *   
  13.  *     @Target 表示該註解用於什麼地方,可能的 ElemenetType 參數包括:
     
  14.  *         ElemenetType.CONSTRUCTOR 構造器聲明
     
  15.  *         ElemenetType.FIELD 域聲明(包括 enum 執行個體)
     
  16.  *         ElemenetType.LOCAL_VARIABLE 局部變數聲明
     
  17.  *         ElemenetType.METHOD 方法聲明  
  18.  *         ElemenetType.PACKAGE 包聲明  
  19.  *         ElemenetType.PARAMETER 參數聲明  
  20.  *         ElemenetType.TYPE 類,介面(包括註解類型)或enum聲明
     
  21.  *           
  22.  *     @Retention 表示在什麼層級儲存該註解資訊。可選的 RetentionPolicy 參數包括:
     
  23.  *         RetentionPolicy.SOURCE 註解將被編譯器丟棄
     
  24.  *         RetentionPolicy.CLASS 註解在class檔案中可用,但會被VM丟棄
     
  25.  *         RetentionPolicy.RUNTIME VM將在運行期也保留注釋,因此可以通過反射機制讀取註解的資訊。
     
  26.  *           
  27.  *     @Documented 將此註解包含在 javadoc 中  
  28.  *       
  29.  *     @Inherited 允許子類繼承父類中的註解  
  30.  *     
  31.  */  
  32. @Target(ElementType.METHOD)   
  33. @Retention(RetentionPolicy.RUNTIME)   
  34. @Documented  
  35. @Inherited  
  36. /*  
  37.  * 定義註解 Test  
  38.  * 註解中含有兩個元素 id 和 description  
  39.  * description 元素 有預設值 "no description"  
  40.  */  
  41. public @interface Test {   
  42.     public int id();   
  43.     public String description() default "no description";   
  44. }  
package Test_annotation;import java.lang.annotation.Documented;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.RetentionPolicy;/* * 元註解@Target,@Retention,@Documented,@Inherited *  *     @Target 表示該註解用於什麼地方,可能的 ElemenetType 參數包括: *         ElemenetType.CONSTRUCTOR 構造器聲明 *         ElemenetType.FIELD 域聲明(包括 enum 執行個體) *         ElemenetType.LOCAL_VARIABLE 局部變數聲明 *         ElemenetType.METHOD 方法聲明 *         ElemenetType.PACKAGE 包聲明 *         ElemenetType.PARAMETER 參數聲明 *         ElemenetType.TYPE 類,介面(包括註解類型)或enum聲明 *          *     @Retention 表示在什麼層級儲存該註解資訊。可選的 RetentionPolicy 參數包括: *         RetentionPolicy.SOURCE 註解將被編譯器丟棄 *         RetentionPolicy.CLASS 註解在class檔案中可用,但會被VM丟棄 *         RetentionPolicy.RUNTIME VM將在運行期也保留注釋,因此可以通過反射機制讀取註解的資訊。 *          *     @Documented 將此註解包含在 javadoc 中 *      *     @Inherited 允許子類繼承父類中的註解 *    */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited/* * 定義註解 Test * 註解中含有兩個元素 id 和 description * description 元素 有預設值 "no description" */public @interface Test {public int id();public String description() default "no description";}

   

下面是一個使用註解 和 解析註解的執行個體

 

Java代碼
  1. package Test_annotation;   
  2.   
  3. import java.lang.reflect.Method;   
  4.   
  5. public class Test_1 {   
  6.     /*  
  7.      * 被註解的三個方法  
  8.      */  
  9.     @Test(id = 1, description = "hello method_1")   
  10.     public void method_1() {   
  11.     }   
  12.   
  13.     @Test(id = 2)   
  14.     public void method_2() {   
  15.     }   
  16.   
  17.     @Test(id = 3, description = "last method")   
  18.     public void method_3() {   
  19.     }   
  20.   
  21.     /*  
  22.      * 解析註解,將Test_1類 所有被註解方法 的資訊列印出來  
  23.      */  
  24.     public static void main(String[] args) {   
  25.         Method[] methods = Test_1.class.getDeclaredMethods();   
  26.         for (Method method : methods) {   
  27.             /*  
  28.              * 判斷方法中是否有指定註解類型的註解  
  29.              */  
  30.             boolean hasAnnotation = method.isAnnotationPresent(Test.class);   
  31.             if (hasAnnotation) {   
  32.                 /*  
  33.                  * 根據註解類型返回方法的指定類型註解  
  34.                  */  
  35.                 Test annotation = method.getAnnotation(Test.class);   
  36.                 System.out.println("Test( method = " + method.getName()   
  37.                         + " , id = " + annotation.id() + " , description = "  
  38.                         + annotation.description() + " )");   
  39.             }   
  40.         }   
  41.     }   
  42.   
  43. }  
package Test_annotation;import java.lang.reflect.Method;public class Test_1 {/* * 被註解的三個方法 */@Test(id = 1, description = "hello method_1")public void method_1() {}@Test(id = 2)public void method_2() {}@Test(id = 3, description = "last method")public void method_3() {}/* * 解析註解,將Test_1類 所有被註解方法 的資訊列印出來 */public static void main(String[] args) {Method[] methods = Test_1.class.getDeclaredMethods();for (Method method : methods) {/* * 判斷方法中是否有指定註解類型的註解 */boolean hasAnnotation = method.isAnnotationPresent(Test.class);if (hasAnnotation) {/* * 根據註解類型返回方法的指定類型註解 */Test annotation = method.getAnnotation(Test.class);System.out.println("Test( method = " + method.getName()+ " , id = " + annotation.id() + " , description = "+ annotation.description() + " )");}}}}

   

輸出結果如下:    Test( method = method_1 , id = 1 , description = hello method_1 )    Test( method = method_2 , id = 2 , description = no description )    Test( method = method_3 , id = 3 , description = last method )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.