Java-NoSuchMethodException与反射

Java-NoSuchMethodException与反射,java,reflection,Java,Reflection,执行以下操作时,我会收到NoSuchMethodException: operacionDTO.getClass().getMethod("setPrioridad").invoke(operacionDTO, 0); java.lang.NoSuchMethodException: xxxx.api.service.dto.RegasificacionDTO.setPrioridad() 但是类Regasificacondo确实有一个名为setPrioridInt i的公共方法,如果在调试

执行以下操作时,我会收到NoSuchMethodException:

operacionDTO.getClass().getMethod("setPrioridad").invoke(operacionDTO, 0);

java.lang.NoSuchMethodException: xxxx.api.service.dto.RegasificacionDTO.setPrioridad()
但是类Regasificacondo确实有一个名为setPrioridInt i的公共方法,如果在调试时调用:

operacionDTO.getClass().getMethods()

然后我得到一个方法数组,其中有一个setPrioridad。我尝试了一些其他类似的方法,但得到了相同的错误。

您需要包含参数签名

 operacionDTO.getClass().getMethod("setPrioridad", Integer.TYPE)

您需要包括参数签名

 operacionDTO.getClass().getMethod("setPrioridad", Integer.TYPE)
方法getMethod接受方法名和参数类型的varargs数组。在您的情况下,您应该调用getMethodsetPrioridad,int.class,一切都会正常工作

这是因为在java中,正如在大多数面向对象语言中一样,您可以使用相同的名称和不同的签名定义多个方法,因此系统使用给定的参数类型来区分它们

方法getMethod接受方法名和参数类型的varargs数组。在您的情况下,您应该调用getMethodsetPrioridad,int.class,一切都会正常工作

 operacionDTO.getClass().getMethod("setPrioridad",new Class[]{Integer.TYPE or Integer.class}).invoke(operacionDTO, 0);
这是因为在java中,正如在大多数面向对象语言中一样,您可以使用相同的名称和不同的签名定义多个方法,因此系统使用给定的参数类型来区分它们

 operacionDTO.getClass().getMethod("setPrioridad",new Class[]{Integer.TYPE or Integer.class}).invoke(operacionDTO, 0);