描述:
- 我需要使用
getMethod,它需要参数类型。
- origin 方法需要
double(原始类型,而不是Double),并且我无法更改 origin 方法。
- 我不能只输入
double.class参数s类型,因为可能不同的类型,例如Integer(not int)。
- 中的方法参数
Foo.java始终且仅是原始类型。
代码:
test.java
public static void main( String args[] )
{
Object obj = new Foo();
Object s = 1.2;
String type = "Double";
try {
Method method = obj.getClass().getMethod("return" + type, s.getClass());// got NoSuchMethodException here, because it requires `double` not Double
System.out.println(method.invoke(obj,s));
} catch (NoSuchMethodException | IllegalAccessException |InvocationTargetException e) {
e.printStackTrace();
}
}
}
Foo.java //(I …
Run Code Online (Sandbox Code Playgroud)