欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java Annotations注解使用示例,javaannotations,如下是使用java注解的

来源: javaer 分享于  点击 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();  }}
相关栏目:

用户点评