http://jdk.java2000.net/doc/jdk6_cn/java/lang/Class.html#getMethod(java.lang.String,%20java.lang.Class...)public Method getMethod(String name,
                        Class<?>... parameterTypes)
                 throws NoSuchMethodException,
                        SecurityException    返回一个 Method 对象,它反映此 Class 对象所表示的类或接口的指定公共成员方法。name 参数是一个 String,用于指定所需方法的简称。parameterTypes 参数是按声明顺序标识该方法形参类型的 Class 对象的一个数组。如果 parameterTypes 为 null,则按空数组处理。    如果 name 是 "<init>;" 或 "<clinit>",则将引发 NoSuchMethodException。否则,要反映的方法由下面的算法确定(设 C 为此对象所表示的类):       1. 在 C 中搜索任一匹配的方法。如果找不到匹配的方法,则将在 C 的超类上递归调用第 1 步算法。
       2. 如果在第 1 步中没有找到任何方法,则在 C 的超接口中搜索匹配的方法。如果找到了这样的方法,则反映该方法。    在 C 类中查找匹配的方法:如果 C 正好声明了一个具有指定名称的公共方法并且恰恰有相同的形参类型,则它就是反映的方法。如果在 C 中找到了多个这样的方法,并且其中有一个方法的返回类型比其他方法的返回类型都特殊,则反映该方法;否则将从中任选一个方法。    注意,类中可以有多个匹配方法,因为尽管 Java 语言禁止类声明带有相同签名但不同返回类型的多个方法,但 Java 虚拟机并不禁止。这增加了虚拟机的灵活性,可以用来实现各种语言特性。例如,可以使用桥方法 (brige method)实现协变返回;桥方法以及将被重写的方法将具有相同的签名,不同的返回类型。 

解决方案 »

  1.   

    谢谢二楼..  我知道啊.  但我很想要的是Method method=obj.getClass().getMethod("execute", ??????); 
    method.invoke(obj,??????); 
    这两句代码里是两个什么参数值  比如:
      Method method=obj.getClass().getMethod("execute", new Class[]{HttpServletRequest.class, HttpServletResponse.class}); 
    但上面的参数肯定是错的
      

  2.   

    Method method=obj.getClass().getMethod("execute", ??????); 红色参数是表示你所要调用的方法的名,
    绿色则是getMethod所代表调用的那方法所需要的参数。
      

  3.   

    谢谢 我知道. 我只是想问下,我上面的那个 execute方法 参数是 HttpServletRequest req, HttpServletResponse resp
    就是不晓得
    Method method=obj.getClass().getMethod("execute", ??????);  那个问候里要输入的值是什么?
     
      

  4.   

    Method method=obj.getClass().getMethod("execute", new Object[]{request, response}); 
      

  5.   

    Method method=obj.getClass().getMethod("execute", new Object[]{request, response}); 
    ---------------------
    websharp, the simple and the best web technology. <a href="http://blog.csdn.net/coolmasoft">http://blog.csdn.net/coolmasoft</a>
      

  6.   

    楼上的,先谢谢你的回复,但你的参数是错误的new Object[]{request, response} 只能是new Class[]{...}
      

  7.   

    搞出来咯,
      Method method=obj.getClass().getMethod("execute", new Class[]{HttpServletRequest.class, HttpServletResponse.class}); 
    method.invoke(obj,new Object[]{reques,reponse}); 
      

  8.   


    java2000_net 把API文档都贴出来了,看这个不就得咯,不要想当然啊这样就得了
    Method method=obj.getClass().getMethod("execute", HttpServletRequest, HttpServletResponse); method.invoke(obj, reques, reponse); 
      

  9.   

    漏了点
    Method method=obj.getClass().getMethod("execute", HttpServletRequest.class, HttpServletResponse.class);