9.
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, "123");
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)
10.
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
List<Object> list = new ArrayList<Object>();
list.getClass().getMethod("add", Object.class).invoke(list, new Object());
System.out.println(list.get(0));
}
正常:
打印:java.lang.Object@1888759
11.
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, new Object());
System.out.println(list.get(0));
}
正常打印出:java.lang.Object@4a5ab2
12.
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, new Integer(1));
System.out.println(list.get(0));
}
正常打印出:1
13.
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, new Long(1));
System.out.println(list.get(0));
}
正常打印出:1
14.
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, new Long(1));
System.out.println(list.get(0).getClass());
}
异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at cn.leo.Game.main(Game.java:31)
15.
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, new Game());
System.out.println(list.get(0));
}
正常打印:cn.leo.Game@6e1408
16.
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, new Game());
System.out.println(list.get(0).getClass());
}
异常:
Exception in thread "main" java.lang.ClassCastException: cn.leo.Game cannot be cast to java.lang.Integer
at cn.leo.Game.main(Game.java:31)
|