- 自定义注解本质上是一个接口,默认继承 Annotation 接口类
- 反编译后可得:public interface com.example.baseexercise.atInterfaceTest.MyAnnotation extends java.lang.annotation.Annotation{
- 注解的属性是接口中的抽象方法
- 反编译后可得:public abstract int intvalue();
- isAnnotationPresent方法与getAnnotation方法是Field的方法,需要通过class反射获取
@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.FIELD)
public @interface MyAnnotation {
MyAnnotation2 myAnnotation2();
int intvalue();
String strValue();
}
- 测试类:
public class AnnotationTest {
@MyAnnotation(myAnnotation2 = @MyAnnotation2(value = 3), intvalue = 2, strValue = "3")
private String testStr;
@SneakyThrows
public static void main(String[] args) throws NoSuchFieldException {
Field field = AnnotationTest.class.getDeclaredField("testStr");
field.setAccessible(true);
if (field.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation annotation = field.getAnnotation(MyAnnotation.class);
System.out.println("我知道MyAnnotation2的value了,值为:"+annotation.myAnnotation2().value());
}else{
System.out.println("我啥都不知道");
}
}
}
- 反编译后的自定义注解
public interface com.example.baseexercise.atInterfaceTest.MyAnnotation extends java.lang.annotation.Annotation {
public abstract com.example.baseexercise.atInterfaceTest.MyAnnotation2 myAnnotation2();
public abstract int intvalue();
public abstract java.lang.String strValue();
}
留言:老婆做了一个小红书账号,全是瓷砖方面的干货,大佬们如果有需求或者有兴趣可以移步了解一下,嘻嘻~
小红书地址,GO GO GO!!!
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a8f57f6c5b32b3015301c10fc66aedba.png)