黑马程序员技术交流社区
标题:
反射练习
[打印本页]
作者:
fmi110
时间:
2015-10-1 15:44
标题:
反射练习
a
import java.lang.reflect.Method;
import java.util.ArrayList;
public class T9 {
/**
* 向泛型为Integer的ArrayList中添加一个String类型元素
* @throws Exception
* @throws NoSuchMethodException
*/
public static void main(String[] args) throws NoSuchMethodException, Exception {
ArrayList<Integer> al = new ArrayList<Integer>();
Class cl = al.getClass();
Method add = cl.getMethod("add", Object.class);
add.invoke(al, "hell java");
System.out.println(al);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2