问个反射的问题,
首先,我定义了一个注解名为W
- Java code
@Target({METHOD, CONSTRUCTOR, FIELD, PARAMETER})@Retention(RUNTIME)public @interface W{ String value() default "WANGNING";}
然后在类中的方法前面加上该注解
- Java code
public class Demo{ @W() public void setMethod(){ }}
最后,我新建一个类,用于输出该注解
- Java code
public class RunTests{ public static void main(String[] args) throws Exception { for (Method m : Class.forName("homework.Demo").getMethods()) { if(m.isAnnotationPresent(W.class)){ System.out.println(m.getAnnotation(W.class)); System.out.println(m.getDefaultValue()); } } }}
我的问题是,m.getDefaultValue()为什么是null,我的注解里有缺省值啊
------解决方案--------------------
你试试 W.class.getMethod("value").getDefaultValue(),这下你能理解了吧?