Java Annotation之介绍篇 (2)

我们在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的应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值