先定义一个注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface HelloAnnotation {
String say() default "Hi";
}
然后在Main函数中解析注解
@HelloAnnotation(say = "Do it!")
public class TestMain {
public static void main(String[] args) {
System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
HelloAnnotation annotation = TestMain.class.getAnnotation(HelloAnnotation.class);
System.out.println(annotation.say());//调用注解对象的say方法,并打印到控制台
}
}