读书人

求指教,Java反射有关问题

发布时间: 2012-03-31 13:13:26 作者: rapoo

求指教,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)

读书人网 >Java Web开发

热点推荐