1

执行时我得到一个NoSuchMethodException

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

java.lang.NoSuchMethodException: xxxx.api.service.dto.RegasificacionDTO.setPrioridad()

但是类RegasificacionDTO确实有一个名为的公共方法setPrioridad(int i),如果在调试时我调用:

operacionDTO.getClass().getMethods()

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

4

3 回答 3

11

您需要包含参数签名。

 operacionDTO.getClass().getMethod("setPrioridad", Integer.TYPE)
于 2013-04-11T08:55:53.127 回答
4

方法getMethod()接受方法名称和参数类型的可变参数数组。在你的情况下,你应该打电话getMethod("setPrioridad", int.class),一切都会奏效。

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

于 2013-04-11T08:57:29.400 回答
1
 operacionDTO.getClass().getMethod("setPrioridad",new Class[]{Integer.TYPE or Integer.class}).invoke(operacionDTO, 0);
于 2013-04-11T08:57:57.197 回答