JAVA自定义注解
1 package annotation; 2 3 import java.lang.annotation.Retention; 4 import java.lang.annotation.RetentionPolicy; 5 //注解可以简单的理解为标签 6 @Retention(RetentionPolicy.RUNTIME) //原注解 7 //定义一个自定义的注解A 8 @interface A{ 9 10 int id() default -1; //默认设置为 -1 11 String name() default "zhao"; 12 } 13 @A 14 public class custom_annotation { 15 16 static void getA(){ 17 18 A a=custom_annotation.class.getAnnotation(A.class); 19 System.out.println(a.id()); 20 System.out.println(a.name()); 21 } 22 23 public static void main(String[] args) { 24 //通过 Class 对象的 isAnnotationPresent() 方法判断它是否应用了某个注解 25 Boolean b=custom_annotation.class.isAnnotationPresent(A.class); 26 if(b){ 27 28 getA(); 29 } 30 } 31 32 }
运行效果

参考:https://blog.csdn.net/briblue/article/details/73824058

浙公网安备 33010602011771号