Java自訂註解

來源:互聯網
上載者:User

標籤:字串   rgs   方法   arraylist   ceo   ace   print   interface   isa   

註解類:

 1 package com.www.yanwu.Annotation; 2  3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Inherited; 6 import java.lang.annotation.Retention; 7 import java.lang.annotation.RetentionPolicy; 8 import java.lang.annotation.Target; 9 10 11 /** 12 * @ClassName: Demo2 13 * @Description: 自訂註解14 * @author Harvey15 * @date 2017年7月27日 下午9:42:21 16 *  17 */18 @Inherited  19 @Documented 20 @Target(value={ElementType.TYPE,ElementType.METHOD})21 @Retention(RetentionPolicy.RUNTIME)22 public @interface Demo2 {23     String studentName() default "";24     int age() default 0;25     int id() default -1;26 }
View Code

@Target註解,作用範圍

@Retention註解可以在定義註解時為編譯器提供註解的保留原則(不填預設CLASS層級)。

CLASS編譯器將把注釋記錄在類檔案中,但在運行時 VM 不需要保留注釋。RUNTIME編譯器將把注釋記錄在類檔案中,在運行時 VM 將保留注釋,因此可以反射性地讀取。SOURCE編譯器要丟棄的注釋。

 

測試註解類:

 1 package com.www.yanwu.Annotation; 2  3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.List; 6 /**  7 * @ClassName: Demo1 8 * @Description: 測試 9 * @author Harvey10 * @date 2017年7月27日 下午9:42:21 11 *  12 */13 @Demo2(age=100,id=20,studentName="hello")14 public class Demo1 {15 16     @Override17     public String toString() {18         // TODO Auto-generated method stub19         return super.toString();20     }21     22     @Demo2(age=10,id=2,studentName="harvey")23     public void test001(){24         System.out.println("hello");25     }26     @SuppressWarnings("all")27     public static void main(String[] args) {28         Date d=new Date();29         //d.parse("2017");30         //test001();31         System.out.println(d);32         List list=new ArrayList();33     }34     35 36 }
View Code

 

解析註解:

 1 package com.www.yanwu.Annotation; 2  3  4 import java.lang.annotation.Annotation; 5 import java.lang.reflect.Method; 6  7 /**  8 * @ClassName: Demo3  9 * @Demo3: TODO(這裡用一句話描述這個類的作用) 10 * @author Harvey11 * @date 2017年7月27日 下午9:42:58 12 *  13 */14 public class Demo3 {15     public static void test() throws ClassNotFoundException {16         17     }18     19     public static void main(String[] args) throws ClassNotFoundException {20          /* 21          * 1.使用類載入器載入類 22          * Class.forName("類名字串") (注意:類名字串必須是全稱,包名+類名) 23          */  24         Class c = Class.forName("com.www.yanwu.Annotation.Demo1");  25           26         //2.判斷類上是否存在註解,並擷取類上面註解的執行個體  27         if(c.isAnnotationPresent(Demo2.class)){  28             Demo2 d = (Demo2) c.getAnnotation(Demo2.class);  29             System.out.println(d.studentName());  30             System.out.println(d.age());  31             System.out.println(d.id());  32         }  33           34         //3.判斷方法上是否存在註解,並擷取方法上面註解的執行個體  35         System.out.println("===========================");36         Method[] ms = c.getMethods();  37         for (Method method : ms) {  38             if(method.isAnnotationPresent(Demo2.class)){  39                 Demo2 d = (Demo2)method.getAnnotation(Demo2.class);  40                 System.out.println(method.getName());41                 System.out.println(d.studentName());  42                 System.out.println(d.age());  43                 System.out.println(d.id());    44             }  45         }  46         //另一種擷取方法上的註解的解析方法  47         System.out.println("===========================");48         for (Method method : ms) {  49             Annotation[] as = method.getAnnotations();  50             for (Annotation annotation : as) {  51                 if(annotation instanceof Demo2){  52                     System.out.println(((Demo2) annotation).id());  53                     System.out.println(((Demo2) annotation).studentName());  54                     System.out.println(((Demo2) annotation).age());  55                 }  56             }  57         }  58         59         System.out.println("hello");60     }61         62     63 }
View Code

 

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.