import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface myAnnotation { String name() default "abc"; }
import java.lang.annotation.Annotation; @myAnnotation public class A { @myAnnotation public void a(){ } public static void main(String[] args) throws Exception { System.out.println("测试annotation的默认值"); A b = new A(); myAnnotation annotations = b.getClass().getMethod("a",null).getAnnotation(myAnnotation.class); System.out.println(annotations.name().toString()); } }