我們在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的應用。