Java自定義注解Annotation詳解


注解相當於一種標記,在程序中加了注解就等於為程序打上了某種標記,沒加,則等於沒有某種標記,以后,javac編譯器,開發工具和其他程序可以用反射來了解你的類及各種元素上有無何種標記,看你有什么標記,就去干相應的事。標記可以加在包,類,字段,方法,方法的參數以及局部變量上。

自定義注解及其應用
1)、定義一個最簡單的注解
public @interface MyAnnotation {
    //......
}
2)、把注解加在某個類上:
@MyAnnotation
public class AnnotationTest{

    //......
}

Java中提供了四種元注解,專門負責注解其他的注解,分別如下:

@Retention元注解,表示需要在什么級別保存該注釋信息(生命周期)。可選的RetentionPoicy參數包括:

RetentionPolicy.SOURCE: 停留在java源文件,編譯器被丟掉

RetentionPolicy.CLASS:停留在class文件中,但會被VM丟棄(默認)

RetentionPolicy.RUNTIME:內存中的字節碼,VM將在運行時也保留注解,因此可以通過反射機制讀取注解的信息

@Target元注解,默認值為任何元素,表示該注解用於什么地方。可用的ElementType參數包括

ElementType.CONSTRUCTOR: 構造器聲明

ElementType.FIELD: 成員變量、對象、屬性(包括enum實例)

ElementType.LOCAL_VARIABLE: 局部變量聲明

ElementType.METHOD: 方法聲明

ElementType.PACKAGE: 包聲明

ElementType.PARAMETER: 參數聲明

ElementType.TYPE: 類、接口(包括注解類型)或enum聲明

@Documented注解將注解包含在JavaDoc中

@Inheried注解允許子類繼承父類中的注解

以下寫了一個模擬注解的案例:

首先寫一個自定義注解@MyAnnotation,代碼如下所示:

package com.pcict.anotation.test;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface MyAnnotation {
    // 為注解添加屬性
    String color();

    String value() default "我是XXX"; // 為屬性提供默認值

    int[] array() default { 1, 2, 3 };

    Gender gender() default Gender.MAN; // 添加一個枚舉

    // 添加枚舉屬性
    MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday = "我的出身日期為1988-2-18");
}

寫一個枚舉類Gender,模擬注解中添加枚舉屬性,代碼如下所示:

package com.pcict.anotation.test;

public enum Gender {
    MAN {
        public String getName() {
            return "男";
        }
    },
    WOMEN {
        public String getName() {
            return "女";
        }
    }; // 后面記得有“;”
    public abstract String getName();
}

寫一個注解類MetaAnnotation,模擬注解中添加注解屬性,代碼如下:

package com.pcict.anotation.test;

public @interface MetaAnnotation {
    String birthday();
}

最后寫注解測試類AnnotationTest,代碼如下:

package com.pcict.anotation.test;

// 調用注解並賦值
@MyAnnotation(metaAnnotation = @MetaAnnotation(birthday = "我的出身日期為1988-2-18"), color = "red", array = {
        23, 26 })
public class AnnotationTest {

    public static void main(String[] args) {
        // 檢查類AnnotationTest是否含有@MyAnnotation注解
        if (AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)) {
            // 若存在就獲取注解
            MyAnnotation annotation = (MyAnnotation) AnnotationTest.class
                    .getAnnotation(MyAnnotation.class);
            System.out.println(annotation);
            // 獲取注解屬性
            System.out.println(annotation.color());
            System.out.println(annotation.value());
            // 數組
            int[] arrs = annotation.array();
            for (int arr : arrs) {
                System.out.println(arr);
            }
            // 枚舉
            Gender gender = annotation.gender();
            System.out.println("性別為:" + gender);
            // 獲取注解屬性
            MetaAnnotation meta = annotation.metaAnnotation();
            System.out.println(meta.birthday());
        }
    }
}

運行AnnotationTest,輸出結果為:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM