Java註解Annotation詳解

來源:互聯網
上載者:User

標籤:

註解相當於一種標記,在程式中加了註解就等於為程式打上了某種標記,沒加,則等於沒有某種標記,以後,javac編譯器,開發工具和其他程式可以用反射來瞭解你的類及各種元素上有無何種標記,看你有什麼標記,就去幹相應的事。標記可以加在包,類,欄位,方法,方法的參數以及局部變數上。    自訂註解及其應用1)、定義一個最簡單的註解public @interface MyAnnotation {    //......}2)、把註解加在某個類上:@MyAnnotationpublic class AnnotationTest{    //......}    以下為類比案例自訂註解@MyAnnotation
 1 package com.ljq.test;
2
3  import java.lang.annotation.ElementType;
4  import java.lang.annotation.Retention;
5  import java.lang.annotation.RetentionPolicy;
6  import java.lang.annotation.Target;
7
8  /**
9 * 定義一個註解
10 *
11 *
12 * @author jiqinlin
13 *
14 */
15  //Java中提供了四種元註解,專門負責註解其他的註解,分別如下
16
17  //@Retention元註解,表示需要在什麼層級儲存該注釋資訊(生命週期)。可選的RetentionPoicy參數包括:
18  //RetentionPolicy.SOURCE: 停留在java源檔案,編譯器被丟掉
19  //RetentionPolicy.CLASS:停留在class檔案中,但會被VM丟棄(預設)
20  //RetentionPolicy.RUNTIME:記憶體中的位元組碼,VM將在運行時也保留註解,因此可以通過反射機制讀取註解的資訊
21
22  //@Target元註解,預設值為任何元素,表示該註解用於什麼地方。可用的ElementType參數包括
23  //ElementType.CONSTRUCTOR: 構造器聲明
24  //ElementType.FIELD: 成員變數、對象、屬性(包括enum執行個體)
25  //ElementType.LOCAL_VARIABLE: 局部變數聲明
26  //ElementType.METHOD: 方法聲明
27  //ElementType.PACKAGE: 包聲明
28  //ElementType.PARAMETER: 參數聲明
29  //ElementType.TYPE: 類、介面(包括註解類型)或enum聲明
30
31  //@Documented將註解包含在JavaDoc中
32
33  //@Inheried允許子類繼承父類中的註解
34  
35
36 @Retention(RetentionPolicy.RUNTIME)
37 @Target({ElementType.METHOD, ElementType.TYPE})
38  public @interface MyAnnotation {
39 //為註解添加屬性
40   String color();
41 String value() default "我是林計欽"; //為屬性提供預設值
42   int[] array() default {1, 2, 3};
43 Gender gender() default Gender.MAN; //添加一個枚舉
44   MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday="我的出身日期為1988-2-18");
45 //添加枚舉屬性
46  
47 }
    註解測試類別AnnotationTest
 1 package com.ljq.test;
2
3  /**
4 * 註解測試類別
5 *
6 *
7 * @author jiqinlin
8 *
9 */
10  //調用註解並賦值
11  @MyAnnotation([email protected](birthday = "我的出身日期為1988-2-18"),color="red", array={23, 26})
12  public class AnnotationTest {
13
14 public static void main(String[] args) {
15 //檢查類AnnotationTest是否含有@MyAnnotation註解
16   if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
17 //若存在就擷取註解
18   MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
19 System.out.println(annotation);
20 //擷取註解屬性
21   System.out.println(annotation.color());
22 System.out.println(annotation.value());
23 //數組
24   int[] arrs=annotation.array();
25 for(int arr:arrs){
26 System.out.println(arr);
27 }
28 //枚舉
29   Gender gender=annotation.gender();
30 System.out.println("性別為:"+gender);
31 //擷取註解屬性
32   MetaAnnotation meta=annotation.metaAnnotation();
33 System.out.println(meta.birthday());
34 }
35 }
36 }
     枚舉類Gender,類比註解中添加枚舉屬性
 1 package com.ljq.test;
2  /**
3 * 枚舉,類比註解中添加枚舉屬性
4 *
5 * @author jiqinlin
6 *
7 */
8  public enum Gender {
9 MAN{
10 public String getName(){return "男";}
11 },
12 WOMEN{
13 public String getName(){return "女";}
14 }; //記得有“;”
15   public abstract String getName();
16 }
     註解類MetaAnnotation,類比註解中添加註解屬性
 1 package com.ljq.test;
2
3  /**
4 * 定義一個註解,類比註解中添加註解屬性
5 *
6 * @author jiqinlin
7 *
8 */
9  public @interface MetaAnnotation {
10 String birthday();
11 }

 

    

Java註解Annotation詳解

聯繫我們

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