myAnnotation
package com.wzh.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface myAnnotation {
String name() default "abc";
//默认的值
String value();
}
使用:
package com.wzh.test;
@myAnnotation("v")
public class Student {
public static void main(String[] args) {
// 如何通过反射获取注解信息
Class<?> c = Student.class;
myAnnotation ma = c.getAnnotation(myAnnotation.class);
System.out.println(ma);
boolean flag= c.isAnnotationPresent(myAnnotation.class);
System.out.println(flag);
System.out.println(ma.value());
System.out.println(ma.name());
}
}