Class,该怎么解决

ClassJava codeimport java.lang.reflect.*public class Test {public static void main(String[] args)

Class

Java code
import java.lang.reflect.*;public class Test {    public static void main(String[] args) {        Class c=null;        try {            c=Class.forName("day16.Student");            Method[] m=c.getDeclaredMethods();            for(int i=0;i<m.length;i++){                author ann=m[i].getAnnotation(Class.forName("author"));                System.out.println(m[i].getName()+":"+ann.value()+":"+ann.date());            }        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }}


Java code
import java.lang.annotation.*;@Documented@Inherited@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD,ElementType.TYPE})public @interface author {    public String value();    public String date();    }


author ann=m[i].getAnnotation(Class.forName("author"));
这句话报错,换做author ann=m[i].getAnnotation(author.class);就OK了,为什么呢,author定义为一个annotation,不可以使用Class.forName的形式吗?


[解决办法]
好像是应为泛型的问题啊。
author ann=m[i].getAnnotation(Class.forName("author"));
改成
author ann=m[i].getAnnotation(Class<author>.forName("author"));
试一试
[解决办法]
帮你查了一下API,其中哦用到了泛型,所以要进行强制转化
author ann=m[i].getAnnotation((author)Class.forName("author"));