可重复注解
1、方法
1)在MyAnnotation上声明@Repeateble,成员值为MyAnnotations.class
2)MyAnnotation的Target和Rentention,要与MyAnnotations相同
要使用的地方
@MyAnnotation(value ="Morning")
@MyAnnotation(value ="Good Night")
//JDK8之前,多注解写法
//@MyAnnotations({@MyAnnotation(value = "hello"),@MyAnnotation(value = "Hi")})
class Person {
private Stringname;
private int age;
public Person(String name,int age) {
this.name = name;
this.age = age;
}
......
}
MyAnnotations.class
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
public @interface MyAnnotations {
MyAnnotation[] value();
}
MyAnnotation.class
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
public @interface MyAnnotation {
String value()default "Hello";
}