且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java反射调用某个对象的方法

更新时间:2022-10-03 14:32:07

// 反射调用某个对象的方法
public Object invokeMethod(Object methodObject, String methodName,
Object[] args) throws Exception {
Class ownerClass = methodObject.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(methodObject, args);

}



本文转自 netcorner 博客园博客,原文链接: http://www.cnblogs.com/netcorner/p/4209918.html  ,如需转载请自行联系原作者