Java反射ArrayList.class.getMethod(“get”)返回NoSuchMethodException

Java反射ArrayList.class.getMethod(“get”)返回NoSuchMethodException,java,reflection,arraylist,nosuchmethoderror,Java,Reflection,Arraylist,Nosuchmethoderror,为什么我不能获取ArrayList的“get”方法并调用它 我正在使用反射在嵌套类中进行修改。我的一个类有一个类列表,所以我希望能够使用相同的逻辑来获取和调用get方法 简单来说,失败的线路是 ArrayList.class.getClass().getMethod("get") 但它失败了,给了我一个例外 我知道我可以使用aList.get(),但这不是重点,我需要使用反射,因为这是在一个深度嵌套的类中 TL;博士 如何获取数组列表的“get”方法?请注意,它有两个参数:一个字符串和一个类对

为什么我不能获取ArrayList的“get”方法并调用它

我正在使用反射在嵌套类中进行修改。我的一个类有一个类列表,所以我希望能够使用相同的逻辑来获取和调用get方法

简单来说,失败的线路是

ArrayList.class.getClass().getMethod("get")
但它失败了,给了我一个例外

我知道我可以使用aList.get(),但这不是重点,我需要使用反射,因为这是在一个深度嵌套的类中

TL;博士 如何获取数组列表的“get”方法?

请注意,它有两个参数:一个
字符串和一个
对象的vararg。前者是

参数列表

方法声明的

你需要使用

ArrayList.class.getMethod("get", int.class);
因为
ArrayList#get(int)
方法有一个
int
参数


起初我错过了整个过程

ArrayList.class.getClass().getMethod("get")
          ^     ^ 
          |     |----------------------------- gets Class<Class>
          |----------------------------------- gets Class<ArrayList>
ArrayList.class.getClass().getMethod(“get”)
^     ^ 
||-------------------------------上课
|-----------------------------------上课
.class
已经获取了
ArrayList
class
实例。在上调用
getClass
,将返回Class
Class
Class
实例。您不希望这样。

请注意,它有两个参数:一个
字符串和一个
对象的vararg。前者是

Method methods = ArrayList.class.getMethod("get", int.class);
参数列表

方法声明的

你需要使用

ArrayList.class.getMethod("get", int.class);
因为
ArrayList#get(int)
方法有一个
int
参数


起初我错过了整个过程

ArrayList.class.getClass().getMethod("get")
          ^     ^ 
          |     |----------------------------- gets Class<Class>
          |----------------------------------- gets Class<ArrayList>
ArrayList.class.getClass().getMethod(“get”)
^     ^ 
||-------------------------------上课
|-----------------------------------上课
.class
已经获取了
ArrayList
class
实例。在上调用
getClass
,将返回Class
Class
Class
实例。你不会想要的

Method methods = ArrayList.class.getMethod("get", int.class);
您不需要在.class之后再次调用getClass()方法,因为当您在类名之后编写.class时,它会引用表示给定类的类对象

您不需要在.class之后再次调用getClass()方法,因为当您在类名之后编写.class时,它会引用表示给定类的类对象