我想用反射来调用方法。
方法是:
public String format(String text,int value)
{
return text+" "+value;
}
因此,它有两个参数:String和int。它返回一个String。 我怎么称呼它?
答案 0 :(得分:1)
try {
Class<?> type = Foo.class;
Method method = type.getMethod("format", String.class, int.class);
//as the method is not static, you need to have an instance of the class to be able to invoke the method
Foo instance = new Foo();
String string = (String) method.invoke(instance, "string", 42);
} catch (Exception toBeHandled) {}
并将Foo
替换为您班级的名称