解析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