自定义注解

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();
}
刚刚参加工作,很有很多不懂不会的,发现错误,欢迎指正,谢谢!
原文地址:https://www.cnblogs.com/xd-study/p/13197594.html