黑马程序员技术交流社区
标题:
关于反射和泛型
[打印本页]
作者:
木木三
时间:
2015-9-9 17:09
标题:
关于反射和泛型
字节码文件类型需要加泛型么?Eclipse总是跳出这个!,看着真不舒服。
但是视频里面也没看到加了泛型限定啊,限定是谁谁谁的类类型这种。
import java.lang.reflect.Method;
import java.util.ArrayList;
public class {
public static void main(String[] args){
ArrayList<Integer> al = new ArrayList<Integer>();
//?Class is a raw type. References to generic type Class<T> should be parameterized
Class c = al.getClass();//获取字节码
try{
//?Type safety: The method getMethod(String, Class...) belongs to the raw type Class.
//?References to generic type Class<T> should be parameterized
Method me = c.getMethod("add", Object.class);//获取add方法
me.invoke(al, "Hello Reflect!");//运行方法
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(al);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2