java Annotations注解使用示例,javaannotations,如下是使用java注解的
分享于 点击 45932 次 点评:130
java Annotations注解使用示例,javaannotations,如下是使用java注解的
如下是使用java注解的简单例子:
import java.lang.annotation.Annotation;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.reflect.Method;@Retention(RetentionPolicy.RUNTIME)@interface MyAnno { String str(); int val();}@Retention(RetentionPolicy.RUNTIME)@interface What { String description();}@What(description = "An annotation test class")@MyAnno(str = "MyClass", val = 99)class MyClass { @What(description = "An annotation test method") @MyAnno(str = "Testing", val = 100) public static void myMeth() { MyClass ob = new MyClass(); try { Annotation annos[] = ob.getClass().getAnnotations(); for (Annotation a : annos) System.out.println(a); Method m = ob.getClass().getMethod("myMeth"); annos = m.getAnnotations(); for (Annotation a : annos) System.out.println(a); } catch (NoSuchMethodException exc) { System.out.println("Method Not Found."); } } public static void main(String args[]) { myMeth(); }}
用户点评