1. 程式人生 > >Annotation 深度剖析(二)

Annotation 深度剖析(二)

自定義註解。

 

1.基本註解程式碼如下:

package com.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnot {
    /*
     *定義基本屬性
     */
    String myAnnot();  // 型別屬性名
}

   測試應用其屬性 

package com.annotation;
@MyAnnot(myAnnot = "user")
public class Test {
    public static void main(String[] args) {
        MyAnnot d =  Test.class.getAnnotation(MyAnnot.class);  //採用反射獲取MyAnnot註解
        System.out.println(d.myAnnot());
    }
}

2.為屬性指定預設值(預設值)

  語法:型別 屬性名() default 預設值;

package com.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/***
 * 註解類,介面,列舉
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBtable {
    String tableName() default "name";  //預設值 預設
}

 

測試其應用屬性

package com.annotation;
@DBtable
public class Test2 {
    public static void main(String[] args) {

        DBtable d =  Test.class.getAnnotation(DBtable.class);
        System.out.println(d.tableName());
    }
}

3.value屬性       @MyAnnotation(value = "test3")  用value 的好處是可以簡寫為 @MyAnnotation( "test3") 

package com.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String color() default "bule";
    String value();
}

測試應用其屬性

@MyAnnotation("test3")
public class Test3 {
    public static void main(String[] args) {
        MyAnnotation m = Test3.class.getAnnotation(MyAnnotation.class);
        System.out.println(m.value());
    }
}

4.高階註解

package com.annotation.enums;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MetaAnnotation {
    String value();
}
package com.annotation.enums;

/***
 * 交通訊號燈顏色 列舉
 */
public enum EumTrafficLamp {

    RED,    //紅
    YELLOW, //黃
    GREEN,  //綠
}
package com.annotation.enums;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation1 {
    String color() default "blue";  //顏色,預設屬性
    String value();
    int[] arrayAttr() default {1,2,3};  ///新增一個int型別陣列的屬性,預設屬性
    EumTrafficLamp lamp()default EumTrafficLamp.RED; //新增一個列舉的屬性,預設屬性
    MetaAnnotation annotationAttr() default @MetaAnnotation("xdp");  ////為註解新增一個註解型別的屬性,並指定註解屬性的預設值
}

測試應用屬性如下

package com.annotation.enums;

@MyAnnotation1(color = "red",value = "test",arrayAttr = {2},lamp = EumTrafficLamp.RED,[email protected]("dps"))
public class Test {
    public static void main(String[] args) {
        MyAnnotation1 myAnnotation1 = Test.class.getAnnotation(MyAnnotation1.class);
        System.out.println(myAnnotation1.color() +":"+myAnnotation1.value()+":"+myAnnotation1.arrayAttr().length+":"+myAnnotation1.lamp()+":"+myAnnotation1.annotationAttr().value());
    }
}