本帖最后由 Friendy89 于 2013-8-31 08:00 编辑
往Integer类型的集合中存放String类型的元素在getMethod的参数类型那里为什么用的是Object.class,而不是String.class,- import java.lang.reflect.Method;
- import java.util.ArrayList;
- public class Test04 {
- /**
- * @param args
- * @throws Exception
- * @throws SecurityException
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- ArrayList<Integer> list = new ArrayList<Integer>();
- Method method = list.getClass().getMethod("add", Object.class);//<FONT color=red>这里用String.class为什么会错,为什么要用Object.class,依据是什么</FONT>
- method.invoke(list, "itcast");
- System.out.println(list);
- }
- }
复制代码 |
|