Java Annotation之介紹篇 (2)

來源:互聯網
上載者:User

我們在Java Annotation之介紹篇 (1)裡,比較詳細地介紹了Annotation的作用,定義,JAVA標準Annotation等。本文著重介紹怎麼樣自訂Annotation以及使用自訂的Annotation。
本文不對範例作詳細解釋,有不明白的地方請參考:Java Annotation之介紹篇 (1)。
Annotation是一種特殊的interface。所以可以在annotation裡定義方法,屬性;也可以讓某個類從annotation繼承(implements)。

下面從簡單地範例開始,讓我們一步步加深對annotation的瞭解。

無任何方法/屬性Annotation範例:

MyAnnotation0.java

1.     package com.test.annotation;   

2.       

3.     public @interface MyAnnotation0 {   

4.            

5.     }  

package com.test.annotation; 

public @interface MyAnnotation0 { 

}

MyAnnotation0為一個無任何方法和屬性的annotation。
使用MyAnnotation0:
TestMyAnnotation0.java

1.     @MyAnnotation0  

2.     public class TestMyAnnotation0 {   

3.         @MyAnnotation0  

4.         public void testMethod() {   

5.       

6.         }   

7.     }  

@MyAnnotation0

public class TestMyAnnotation0 {

    @MyAnnotation0

    public void testMethod() { 

    }

}

具有一個value方法Annotation範例:

MyAnnotation1.java

1.     public @interface MyAnnotation1 {   

2.            

3.         /**  

4.          * value method  

5.          * @return value  

6.          */  

7.         public String value();   

8.     }  

public @interface MyAnnotation1 {

      /**

     * value method

     * @return value

     */

    public String value();

}

MyAnnotation1具有一個名為value的方法。
MyAnnotation1使用:
TestMyAnnotation1.java

1.     @MyAnnotation1("hello")   

2.     public class TestMyAnnotation1 {   

3.         @MyAnnotation1(value="world")   

4.         public void testMethod() {   

5.         }   

6.     }  

@MyAnnotation1("hello")

public class TestMyAnnotation1 {

    @MyAnnotation1(value="world")

    public void testMethod() {

    }

}
可以通過@Annotation名(方法名1=值1, 方法名2=值2, …)的形式給annotation賦值。只有一個方法的時候,可以直接省略為:@Annotation名(值1) 的賦值形式。當方法返回一個數組時,可以用 方法名={值1, 值2, …}給其賦值。

 

具有一個value方法和一個屬性Annotation範例:

如果必要,還可以在annotation裡為其定義屬性。如下:
MyAnnotation2.java

1.     @interface MyAnnotation2 {   

2.         public String value();   

3.         public String myProperty = "hello world";   

4.     }  

@interface MyAnnotation2 {

    public String value();

    public String myProperty = "hello world";

}
其中,myProperty只能申明為public或無public修飾(無public修飾時也預設為public)為static, final屬性(即使不寫也預設為static, final)。

使用例:
TestMyAnnotation2

1.     class TestMyAnnotation2 {   

2.         public static void main(String[] args) {   

3.             System.out.println(MyAnnotation2.myProperty);   

4.         }   

5.       

6.         @MyAnnotation2("")   

7.         public void testMethod1() {   

8.         }   

9.     }  

class TestMyAnnotation2 {

    public static void main(String[] args) {

        System.out.println(MyAnnotation2.myProperty);

    } 

    @MyAnnotation2("")

    public void testMethod1() {

    }

}
上例會列印出:

hello world

 

複雜型annotation的定義與使用

本節介紹較為複雜的annotation定義與使用。
先看代碼:
MyAnnotation3.java

1.     public @interface MyAnnotation3 {   

2.         public String value();   

3.         public String[] multiValues();   

4.         int number() default 0;   

5.     }  

public @interface MyAnnotation3 {

    public String value();

    public String[] multiValues();

    int number() default 0;

}
MyAnnotation3具有一個返回String的value方法,返回String[]的multiValues 方法;還有一個返回int 的number方法。其中number方法具有預設值0。

使用例: 
TestMyAnnotation3.java

1.     class TestMyAnnotation3 {   

2.         @MyAnnotation3(value = "call testMethod1", multiValues={"1", "2"}, number = 1)   

3.         public void testMethod1() {   

4.       

5.         }   

6.       

7.         @MyAnnotation3(value = "call testMethod2", multiValues={"1", "2"})   

8.         public void testMethod2() {   

9.       

10.       }   

11.   }  

class TestMyAnnotation3 {

    @MyAnnotation3(value = "call testMethod1", multiValues={"1", "2"}, number = 1)

    public void testMethod1() { 

    } 

    @MyAnnotation3(value = "call testMethod2", multiValues={"1", "2"})

    public void testMethod2() {

     }

}

number具有預設值,所以標註時可以不為其賦值。其餘方法則必須通過上面介紹的方法賦值。multiValues返回一個String[]數組,所以可以通過multiValues={"1", "2"}為其賦值。

本文對怎麼自訂Annotation進行了介紹,我們將在以後的文章裡,著重介紹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.