BBS水木清华站∶精华区

发信人: 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()); 
∶ } 
∶ } 
∶ } 
 
 
-- 
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.78.145] 

BBS水木清华站∶精华区