java Object数组和反射的一些问题!!

mtkof 2013-06-02 05:06:44
public class DumpMethods {

//写一些方法
private Integer Nums(){
int j=5;
int k=3;
return j+k;

}

// 执行该对象的方法
public static Object invokeMethod(Object owner,String methodName,Object [] args)
throws Exception{
Class ownerClass=owner.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(owner, args);

}

public static void main(String []args){

DumpMethods dd=new DumpMethods(str);
/////////////////////////////
invokeMethod(dd, "Nums",???);
}
请问这里要invokeMethod(dd, "Nums",???);如何传参? 本人新手,求高手指点!!
}
...全文
175 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mtkof 2013-06-17
  • 打赏
  • 举报
回复
问题解决了!!多谢! 原来是修饰符的问题。看来我要多补补基础了!
火影之贺 2013-06-17
  • 打赏
  • 举报
回复
引用 7 楼 mtkof 的回复:
大神 ,,,,在么么,求指导
小伙,比较下2个代码的差别就知道大致的问题原因了。 getMethod(String name, Class... parameterTypes) Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object getDeclaredMethods() Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. 你调用private方法,就要用下面一个: Method method = ownerClass.getDeclaredMethod(methodName, argsClass); method.setAccessible(true); return method.invoke(owner, args);
mtkof 2013-06-10
  • 打赏
  • 举报
回复
大神 ,,,,在么么,求指导
mtkof 2013-06-09
  • 打赏
  • 举报
回复

import java.lang.reflect.*;
public class DumpMethods {

	private static Integer Nums(Integer j, Integer k) {
		System.out.println("Nums Executed");
		return j + k;
	}
        
        public static void main(String[] args) {
            try {
			DumpMethods dd = new DumpMethods();
			invokeMethod(dd, "Nums",new Object[]{1,4});
            
		} catch (Throwable e) {
			e.printStackTrace();
			System.err.println(e);
		}

}
               	//3. 执行某对象的方法
	public static Object invokeMethod(Object owner, String methodName,
			Object[] args) throws Exception {

		Class ownerClass = owner.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(owner, args);

	}
	}

oh_Maxy 版主 2013-06-09
  • 打赏
  • 举报
回复
引用 3 楼 mtkof 的回复:
但是抛出:java.lang.NoSuchMethodException: reflection.DumpMethods.Nums(java.lang.Integer, java.lang.Integer) 异常 !!!
把你现在的代码贴出来
mtkof 2013-06-08
  • 打赏
  • 举报
回复
有人么!!! 求大神帮忙解答!!!!
mtkof 2013-06-02
  • 打赏
  • 举报
回复
但是抛出:java.lang.NoSuchMethodException: reflection.DumpMethods.Nums(java.lang.Integer, java.lang.Integer) 异常 !!!
mtkof 2013-06-02
  • 打赏
  • 举报
回复
对 ,没错!是这个意思!!!谢谢啦!!
火影之贺 2013-06-02
  • 打赏
  • 举报
回复
不知道我有没有理解错,感觉你是想在dd上做反射,???只是入参: import java.lang.reflect.Method; public class DumpMethods { // 写一些方法 public static Integer Nums(Integer j, Integer k) { System.out.println("Nums executed"); return j + k; } // 执行该对象的方法 public static void invokeMethod(Object owner, String methodName, Object[] args) throws Exception { Class ownerClass = owner.getClass(); Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); // Method[] m = ownerClass.getMethods(); Method method = ownerClass.getMethod(methodName, new Class[]{Integer.class, Integer.class}); method.invoke(owner, args); } } public static void main(String[] args) throws Exception { DumpMethods dd = new DumpMethods(); // /////////////////////////// invokeMethod(dd, "Nums", new Object[]{3,5}); } }

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧