萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 905|回复: 0

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

[复制链接]
发表于 2003-2-6 16:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x

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

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

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

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

  21. Object[] realArgs = new Object[2];
  22. String[] strArgs = {"aa","bb"};
  23. Integer intArgs = ....;
  24. realArgs[0] = strArgs;
  25. realArgs[1] = intArgs;
  26. thisMethod.invoke(thisClass,realArgs);
  27. ..

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

  31. Thank you, I will try...
  32. 【 在 wingzhang (scott) 的大作中提到: 】
  33. : you may try as following:
  34. : Object[] realArgs = new Object[2];
  35. : String[] strArgs = {"aa","bb"};
  36. : Integer intArgs = ....;
  37. : realArgs[0] = strArgs;
  38. : realArgs[1] = intArgs;
  39. : thisMethod.invoke(thisClass,realArgs);

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

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



  44. 【 在 wingzhang (scott) 的大作中提到: 】
  45. : you may try as following:
  46. : Object[] realArgs = new Object[2];
  47. : String[] strArgs = {"aa","bb"};
  48. : Integer intArgs = ....;
  49. : realArgs[0] = strArgs;
  50. : realArgs[1] = intArgs;
  51. : thisMethod.invoke(thisClass,realArgs);
  52. : ..

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

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

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

  60. import java.lang.reflect.*;

  61. public class TestM {
  62. public static void main(String[] args){
  63.    try{
  64.   TestM t = new TestM();
  65.   Class c = t.getClass();
  66.   Class[] cargs = new Class[2];
  67.   String[] realArgs = {"aa","bb"};
  68.   cargs[0] = realArgs.getClass();
  69.   Integer in = new Integer(2);
  70.   cargs[1] = in.getClass();
  71.   Method m = c.getMethod("test",cargs);
  72.   Object[] inArgs = new Object[2];
  73.   inArgs[0] = readArgs;
  74.   inArgs[1] = in;
  75.   m.invoke(t,inArgs);
  76. }catch(Exception e){System.out.println(e);}
  77. }

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


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

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

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

  99. 【 在 wingzhang (scott) 的大作中提到: 】
  100. : Can you put out your method signature?
  101. : I have passed the following examples:
  102. : import java.lang.reflect.*;
  103. : public class TestM {
  104. : public static void main(String[] args){
  105. :    try{
  106. :   TestM t = new TestM();
  107. :   Class c = t.getClass();
  108. :   Class[] cargs = new Class[2];
  109. :   String[] realArgs = {"aa","bb"};
  110. :   cargs[0] = realArgs.getClass();
  111. :   Integer in = new Integer(2);
  112. :   cargs[1] = in.getClass();
  113. :   Method m = c.getMethod("test",cargs);
  114. :   Object[] inArgs = new Object[2];
  115. :   inArgs[0] = readArgs;
  116. :   inArgs[1] = in;
  117. :   m.invoke(t,inArgs);
  118. :  }catch(Exception e){System.out.println(e);}
  119. : }
  120. :  public void test(String[] str,Integer stri){
  121. :    for(int j = 0; j < str.length; j ++)
  122. :    System.out.println(str[j]);
  123. :    System.out.println(stri.intValue());
  124. : }
  125. : }
  126. : }

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

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

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

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

  150. Thanks a lot!
  151. Success.

  152. 【 在 wingzhang (scott) 的大作中提到: 】
  153. : how about following:
  154. :   Class c = Class.forName("MyMethods");
  155. :   Object obj = c.newInstance();
  156. :   ...
  157. :   so you can have both the object and the method
复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网 |网站地图

GMT+2, 2024-5-22 08:01 , Processed in 0.054260 second(s), 20 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表