A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 尹震 中级黑马   /  2012-3-12 10:46  /  1720 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 尹震 于 2012-3-17 15:06 编辑

@AnnotationInter("aaa") //类注解

   @AnnotationInter("bbb") //方法注解

AnnotationInter annotation = AnnotationTest.class.getAnnotation(AnnotationInter.class);

System.out.println(annotation.value());//结果为"aaa"

想得到"bbb",应该怎么做?

5 个回复

倒序浏览
没人知道吗?
回复 使用道具 举报
尹震 发表于 2012-3-12 17:55
没人知道吗?

?????
回复 使用道具 举报
获得方法注释应该用Method
---AnnotationTest.class.getAnnotation(AnnotationInter.class);
比如方法名叫test
  Class clazz = AnnotationTest.class;
   Method method = clazz.getMethod("test");
AnnotationInter annotation  = method.getAnnotation(AnnotationInter.class);
才行。这个添加在方法上的注释(注解)上必须写上元注释(元注解)@Retention(value=RetentionPolicy.RUNTIME )
回复 使用道具 举报
本帖最后由 OMG 于 2012-3-17 16:14 编辑

首先要先通过方法的名称(如方法名:test )得到Method对象:再通过Method对象得到注解就可以了,如下
Method method= AnnotationTest.class.getMethod("test ");
AnnotationInter annotationInter=method.getAnnotation(AnnotationInter.class);
System.out.println(annotation.value());//
回复 使用道具 举报
多谢两位,问题已解决。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马