我通过反射调用一个带有可变参数的方法是运行失败,代码如下。
public class Main {
public static void func(Object... objs){
for (Object obj : objs){
System.out.println(obj);
}
}
public static class Tester{
public static void main(String... args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> clazz = Class.forName("com.xxxx.Main");
Method method1 = clazz.getMethod("func", Object[].class);
method1.invoke(null, new Object[]{1, "2"});
}
}
}
异常信息如下
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.zeeker.reflect.Main$Tester.main(Main.java:30)
这其中是有什么猫腻?
添加回答
举报
0/150
提交
取消