文选流氓 发表于 2003-2-6 15:51

1-1-4-2 有关于Method getMethod(String,Class[])方法求助!


发信人: sheen (星矢胖胖熊-一切都会有的), 信区: Java      
标题: 有关于Method getMethod(String,Class[])方法求助!
发信站: BBS 水木清华站 (Sun Mar7 09:18:41 1999)

thisMethod.invoke(thisClass, new Object[] {InParam,new Integer(OutParamNum)})
                                           ~~~~~~为String[],而不是String,
所以不能用new String(InParam),程序运行到此句就停,
what should I do?
help!
多谢各位,多谢!

发信人: wingzhang (scott), 信区: Java      
标题: Re: 有关于Method getMethod(String,Class[])方法求助
发信站: BBS 水木清华站 (Sun Mar7 10:07:32 1999)

【 在 sheen (星矢胖胖熊-一切都会有的) 的大作中提到: 】
: thisMethod.invoke(thisClass, new Object[] {InParam,new Integer(OutParamNum)})
:                                          ~~~~~~为String[],而不是String,
: 所以不能用new String(InParam),程序运行到此句就停,
: what should I do?
: help!
: 多谢各位,多谢!
you may try as following:

Object[] realArgs = new Object;
String[] strArgs = {"aa","bb"};
Integer intArgs = ....;
realArgs = strArgs;
realArgs = intArgs;
thisMethod.invoke(thisClass,realArgs);
..

发信人: sheen (星矢胖胖熊-一切都会有的), 信区: Java      
标题: Re: 有关于Method getMethod(String,Class[])方法求助
发信站: BBS 水木清华站 (Sun Mar7 15:49:24 1999)

Thank you, I will try...
【 在 wingzhang (scott) 的大作中提到: 】
: you may try as following:
: Object[] realArgs = new Object;
: String[] strArgs = {"aa","bb"};
: Integer intArgs = ....;
: realArgs = strArgs;
: realArgs = intArgs;
: thisMethod.invoke(thisClass,realArgs);

发信人: sheen (星矢胖胖熊-一切都会有的), 信区: Java      
标题: Re: 有关于Method getMethod(String,Class[])方法求助
发信站: BBS 水木清华站 (Mon Mar8 09:02:06 1999)

I have try, but the same result: 无法调用此方法



【 在 wingzhang (scott) 的大作中提到: 】
: you may try as following:
: Object[] realArgs = new Object;
: String[] strArgs = {"aa","bb"};
: Integer intArgs = ....;
: realArgs = strArgs;
: realArgs = intArgs;
: thisMethod.invoke(thisClass,realArgs);
: ..

发信人: wingzhang (scott), 信区: Java      
标题: Re: 有关于Method getMethod(String,Class[])方法求助
发信站: BBS 水木清华站 (Mon Mar8 10:43:49 1999)

【 在 sheen (星矢胖胖熊-一切都会有的) 的大作中提到: 】
: I have try, but the same result: 无法调用此方法

Can you put out your method signature?
I have passed the following examples:

import java.lang.reflect.*;

public class TestM {
public static void main(String[] args){
   try{
TestM t = new TestM();
Class c = t.getClass();
Class[] cargs = new Class;
String[] realArgs = {"aa","bb"};
cargs = realArgs.getClass();
Integer in = new Integer(2);
cargs = in.getClass();
Method m = c.getMethod("test",cargs);
Object[] inArgs = new Object;
inArgs = readArgs;
inArgs = in;
m.invoke(t,inArgs);
}catch(Exception e){System.out.println(e);}
}

public void test(String[] str,Integer stri){
   for(int j = 0; j < str.length; j ++)
   System.out.println(str);
   System.out.println(stri.intValue());
}
}
}


发信人: sheen (笨笨熊-一切都会有的), 信区: Java      
标题: Re: 有关于Method getMethod(String,Class[])方法求助
发信站: BBS 水木清华站 (Mon Mar8 15:04:30 1999)

Thank you, I have passed it, but not the desire I thought.

原先不成是因为我用的是forName(String)调用我的类,之后没有事例化,这是错误
原因。
我原先设想的是用自己的一个loadMethod(String ClassName,String MethodName)
灵活地调用各个不同的类方法,但未果,因为在invoke(Object,Object[])时,
                                                 ~~~~~~必须指定一个对象,
而在初始化对象事,如 MyMethods obj = new MyMethods();
                     ~~~~~~~~~必须确定类名,这样就不能只通过
一个String ClassName 和 一个 String MethodName ,灵活地调用偶的方法,
现在,偶只好用一个土土的办法,就是将所有的方法放在一个类下,实现之,
仁兄,莫见笑,有何高见,please tell me!

【 在 wingzhang (scott) 的大作中提到: 】
: Can you put out your method signature?
: I have passed the following examples:
: import java.lang.reflect.*;
: public class TestM {
: public static void main(String[] args){
:    try{
:   TestM t = new TestM();
:   Class c = t.getClass();
:   Class[] cargs = new Class;
:   String[] realArgs = {"aa","bb"};
:   cargs = realArgs.getClass();
:   Integer in = new Integer(2);
:   cargs = in.getClass();
:   Method m = c.getMethod("test",cargs);
:   Object[] inArgs = new Object;
:   inArgs = readArgs;
:   inArgs = in;
:   m.invoke(t,inArgs);
:}catch(Exception e){System.out.println(e);}
: }
:public void test(String[] str,Integer stri){
:    for(int j = 0; j < str.length; j ++)
:    System.out.println(str);
:    System.out.println(stri.intValue());
: }
: }
: }

发信人: wingzhang (scott), 信区: Java      
标题: Re: 有关于Method getMethod(String,Class[])方法求助
发信站: BBS 水木清华站 (Mon Mar8 17:58:42 1999)

【 在 sheen (笨笨熊-一切都会有的) 的大作中提到: 】
: Thank you, I have passed it, but not the desire I thought.
: 原先不成是因为我用的是forName(String)调用我的类,之后没有事例化,这是错误
: 原因。
: 我原先设想的是用自己的一个loadMethod(String ClassName,String MethodName)
: 灵活地调用各个不同的类方法,但未果,因为在invoke(Object,Object[])时,
:                                                ~~~~~~必须指定一个对象,
: 而在初始化对象事,如 MyMethods obj = new MyMethods();
:                      ~~~~~~~~~必须确定类名,这样就不能只通过
: 一个String ClassName 和 一个 String MethodName ,灵活地调用偶的方法,
: 现在,偶只好用一个土土的办法,就是将所有的方法放在一个类下,实现之,
: 仁兄,莫见笑,有何高见,please tell me!

how about following:
Class c = Class.forName("MyMethods");
Object obj = c.newInstance();
...
so you can have both the object and the method

发信人: sheen (笨笨熊-一切都会有的), 信区: Java      
标题: Re: 有关于Method getMethod(String,Class[])方法求助
发信站: BBS 水木清华站 (Wed Mar 10 09:34:32 1999)

Thanks a lot!
Success.

【 在 wingzhang (scott) 的大作中提到: 】
: how about following:
:   Class c = Class.forName("MyMethods");
:   Object obj = c.newInstance();
:   ...
:   so you can have both the object and the method
页: [1]
查看完整版本: 1-1-4-2 有关于Method getMethod(String,Class[])方法求助!