当前位置: 代码迷 >> Java Web开发 >> 求指教,Java反射有关问题
  详细解决方案

求指教,Java反射有关问题

热度:269   发布时间:2016-04-17 10:25:33.0
求指教,Java反射问题
如题:
##自定义Annotation##
public @interface MyAnnotation {
 String role();
}

##Users类##
public class User {
 @MyAnnotation(role="aaa")
 public void say()
 {
 System.out.println("HI!");
 }
}

##Main方法##
Class a=Class.forName("Users");
Method [] method=a.getMethods();
for (int i = 0; i < method.length; i++) {
 System.out.println(method[i].getName());
 System.out.println(method[i].getAnnotations().length);
}


结果Say方法为的Annotations为0;
为什么?

------解决方案--------------------
想要在运气时去到注解那么需要设置@Retention(RetentionPolicy.RUNTIME)
  相关解决方案