先贴上我做的实验,大家就明白了,原理楼下讲
1.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<Integer> list = new ArrayList<Integer>();
list.getClass().getMethod("add", Object.class).invoke(list, "abc");
System.out.println(list.get(0));
}
运行结果是:abc
和视频一样,
2.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<Integer> list = new ArrayList<Integer>();
list.getClass().getMethod("add", Object.class).invoke(list, "abc");
System.out.println(list.get(0).getClass());
}
发现出错:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at cn.leo.Game.main(Game.java:31)
3.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<Integer> list = new ArrayList<Integer>();
list.getClass().getMethod("add", Object.class).invoke(list, "abc");
System.out.println(list.get(0).toString());
}
仍然出错:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at cn.leo.Game.main(Game.java:31)
4.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<String> list = new ArrayList<String>();
list.getClass().getMethod("add", Object.class).invoke(list, 123);
System.out.println(list.get(0));
}
抛出异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at cn.leo.Game.main(Game.java:31)
5.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<Integer> list = new ArrayList<Integer>();
list.getClass().getMethod("add", Object.class).invoke(list, "abc");
list.get(0);
}
正常
6.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<String> list = new ArrayList<String>();
list.getClass().getMethod("add", Object.class).invoke(list, 123);
list.get(0);
}
正常
7.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<Integer> list = new ArrayList<Integer>();
list.getClass().getMethod("add", Object.class).invoke(list, "abc");
list.get(0).toString();
}
异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at cn.leo.Game.main(Game.java:31)
8.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<String> list = new ArrayList<String>();
list.getClass().getMethod("add", Object.class).invoke(list, 123);
list.get(0).toString();
}
异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at cn.leo.Game.main(Game.java:31)
|