|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
- 发信人: sheen (星矢胖胖熊-一切都会有的), 信区: Java
- 标 题: 有关于Method getMethod(String,Class[])方法求助!
- 发信站: BBS 水木清华站 (Sun Mar 7 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 Mar 7 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[2];
- String[] strArgs = {"aa","bb"};
- Integer intArgs = ....;
- realArgs[0] = strArgs;
- realArgs[1] = intArgs;
- thisMethod.invoke(thisClass,realArgs);
- ..
- 发信人: sheen (星矢胖胖熊-一切都会有的), 信区: Java
- 标 题: Re: 有关于Method getMethod(String,Class[])方法求助
- 发信站: BBS 水木清华站 (Sun Mar 7 15:49:24 1999)
- Thank you, I will try...
- 【 在 wingzhang (scott) 的大作中提到: 】
- : you may try as following:
- : Object[] realArgs = new Object[2];
- : String[] strArgs = {"aa","bb"};
- : Integer intArgs = ....;
- : realArgs[0] = strArgs;
- : realArgs[1] = intArgs;
- : thisMethod.invoke(thisClass,realArgs);
- 发信人: sheen (星矢胖胖熊-一切都会有的), 信区: Java
- 标 题: Re: 有关于Method getMethod(String,Class[])方法求助
- 发信站: BBS 水木清华站 (Mon Mar 8 09:02:06 1999)
- I have try, but the same result: 无法调用此方法
- 【 在 wingzhang (scott) 的大作中提到: 】
- : you may try as following:
- : Object[] realArgs = new Object[2];
- : String[] strArgs = {"aa","bb"};
- : Integer intArgs = ....;
- : realArgs[0] = strArgs;
- : realArgs[1] = intArgs;
- : thisMethod.invoke(thisClass,realArgs);
- : ..
- 发信人: wingzhang (scott), 信区: Java
- 标 题: Re: 有关于Method getMethod(String,Class[])方法求助
- 发信站: BBS 水木清华站 (Mon Mar 8 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[2];
- String[] realArgs = {"aa","bb"};
- cargs[0] = realArgs.getClass();
- Integer in = new Integer(2);
- cargs[1] = in.getClass();
- Method m = c.getMethod("test",cargs);
- Object[] inArgs = new Object[2];
- inArgs[0] = readArgs;
- inArgs[1] = 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[j]);
- System.out.println(stri.intValue());
- }
- }
- }
- 发信人: sheen (笨笨熊-一切都会有的), 信区: Java
- 标 题: Re: 有关于Method getMethod(String,Class[])方法求助
- 发信站: BBS 水木清华站 (Mon Mar 8 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[2];
- : String[] realArgs = {"aa","bb"};
- : cargs[0] = realArgs.getClass();
- : Integer in = new Integer(2);
- : cargs[1] = in.getClass();
- : Method m = c.getMethod("test",cargs);
- : Object[] inArgs = new Object[2];
- : inArgs[0] = readArgs;
- : inArgs[1] = 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[j]);
- : System.out.println(stri.intValue());
- : }
- : }
- : }
- 发信人: wingzhang (scott), 信区: Java
- 标 题: Re: 有关于Method getMethod(String,Class[])方法求助
- 发信站: BBS 水木清华站 (Mon Mar 8 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
复制代码 |
|