自定义注解

1.3、自定义注解

public class Test03 {
    //对于没有默认值的注解必须显示赋值
    // 有默认值的注解可以不再赋值
    @MyAnnotation2(id = 11)
    public void test(){};
    //注解只有一个参数时 多用value 
    // 可以在配置的地方省略 value =
    @MyAnnotation3("大帅锅")
    public void test2(){};
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    //注解的参数:注解的类型 + 参数名()
    int age() default 18;
    String name() default "帅锅";
    int id() default 0;
    String[] schools() default {"北京大学","清华大学"};
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    String value();
}
posted @ 2020-06-27 10:41  小徐学狂  阅读(103)  评论(0编辑  收藏  举报