注解

1.自定义注解

@SuppressWarnings("unsed")
@myAnnotaion("hi")
public class AnnotaionTest {
    public static void main(String[] args) {
        Class<AnnotaionTest> annotaionTestClass = AnnotaionTest.class;
        Annotation annotation = annotaionTestClass.getAnnotation(myAnnotaion.class);
        System.out.println(annotation);
    }
}
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface myAnnotaion {
    String value();
}

2.继承注解

注解可以被继承

@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Inherited
public @interface myAnnotaion {
    String value();
}

3.重复注解

@Repeatable(myAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Inherited
public @interface myAnnotaion {
    String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Inherited
public @interface myAnnotations {
    myAnnotaion[] value();
}

4.类型注解

可以用在泛型和数据类型上

TYPE_PARAMETER,TYPE_USE

 

posted @ 2022-04-25 21:40  写代码的小哥哥  阅读(24)  评论(0)    收藏  举报