3 回答

TA贡献1963条经验 获得超6个赞
// String.class here is the parameter type, that might not be the case with you
Method method = clazz.getMethod("methodName", String.class);
Object o = method.invoke(null, "whatever");
如果该方法是私有使用getDeclaredMethod()而不是getMethod()。并调用setAccessible(true)方法对象。

TA贡献1796条经验 获得超10个赞
从Method.invoke()的Javadoc中:
如果基础方法是静态的,则忽略指定的obj参数。它可以为空。
当你会发生什么
类别klass = ...;
方法m = klass.getDeclaredMethod(methodName,paramtypes);
m.invoke(null,args)

TA贡献1773条经验 获得超3个赞
String methodName= "...";
String[] args = {};
Method[] methods = clazz.getMethods();
for (Method m : methods) {
if (methodName.equals(m.getName())) {
// for static methods we can use null as instance of class
m.invoke(null, new Object[] {args});
break;
}
}
添加回答
举报