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ˮľÇ廪վ¡Ã¾«»ªÇø