解析Java反射java.lang.IllegalArgumentException: wrong number of arguments


项目中遇到的问题

import
org.springframework.util.ReflectionUtils; import java.lang.reflect.Method; public class Student { public void contextLoads(Student student) throws ClassNotFoundException, NoSuchMethodException { Class<?> aClass = Class.forName("com.hanclouds.starter.cloud.access.test.Student"); Method method = aClass.getMethod("ss", String[].class); String[] ss={"66","898"}; ReflectionUtils.invokeMethod(method, student, (Object) ss); //这里要把ss强转object java方法中只有一个参数是数组,反射的时候我们不能想当然的传歌数组进去,传数组进去的时候表示多个参数。
//两个数组不是一个意思啊。
//我们应该把数组转为objet,这样才表示一个参数。 }
public void ss(String[] ss){ System.out.println("这是反射"+ss[1]); } public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException { Student student =new Student(); student.contextLoads(student); } }

参考

https://www.iteye.com/blog/huangyunbin-2179144


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM