Annotation例子
import java.lang.annotation.Annotation;/** * Annotation例子 * *///Annotation标签@interface MyAnnotation{//属性要与方法的形式声明String AnnotationName();int age();//以default方式指定初始值double number() default 5.6;String name() default "test";}public class Annotaation3 {//属性需要指定初始值@MyAnnotation(AnnotationName = "Ok", age = 0)@TestAnnotian(i = 0, name = "")public void testAn(){}public static void main(String[] args) throws SecurityException, NoSuchMethodException, ClassNotFoundException {//获得Annotaation3类的testAn()方法的所有注释Annotation[] a = Class.forName("Annotaation3").getMethod("testAn").getAnnotations();//遍历所有注释for (Annotation an : a) {System.out.println(an);}}}
?