我需要Method类的getMethod和invoke的详细用法。
我的QQ是76510863
希望那位高手不吝赐教!!!!!
很急!!!!!!!!!

解决方案 »

  1.   

    你是不是那个问函数指针的?
    -------------------------
    getMethod(****)和getMethods()是Class类的方法。不是Method类的。其实这个问题看一下API就足够了。如果你不想找,我帮你找吧。
    ---------------------------------
    getMethod
    public Method getMethod(String name,
                            Class[] parameterTypes)
                     throws NoSuchMethodException,
                            SecurityExceptionReturns a Method object that reflects the specified public member method of the class or interface represented by this Class object. The name parameter is a String specifying the simple name the desired method. The parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If parameterTypes is null, it is treated as if it were an empty array. 
    If there is a security manager, this method first calls the security manager's checkMemberAccess method with this and Member.PUBLIC as its arguments. If the class is in a package, then this method also calls the security manager's checkPackageAccess method with the package name as its argument. Either of these calls could result in a SecurityException. If the name is "<init>"or "<clinit>" a NoSuchMethodException is raised. Otherwise, the method to be reflected is determined by the algorithm that follows. Let C be the class represented by this object: C is searched for any matching methods. If no matching method is found, the algorithm of step 1 is invoked recursively on the superclass of C. 
    If no method was found in step 1 above, the superinterfaces of C are searched for a matching method. If any such method is found, it is reflected. 
    To find a matching method in a class C:  If C declares exactly one public method with the specified name and exactly the same formal parameter types, that is the method reflected. If more than one such method is found in C, and one of these methods has a return type that is more specific than any of the others, that method is reflected; otherwise one of the methods is chosen arbitrarily. 
    See The Java Language Specification, sections 8.2 and 8.4. 
    Parameters:
    name - the name of the method
    parameterTypes - the list of parameters 
    Returns:
    the Method object that matches the specified name and parameterTypes 
    Throws: 
    NoSuchMethodException - if a matching method is not found or if the name is "<init>"or "<clinit>". 
    NullPointerException - if name is null 
    SecurityException - if access to the information is denied.
    Since: 
    JDK1.1 
    See Also:
    Method, SecurityManager.checkMemberAccess(Class, int), SecurityManager.checkPackageAccess(String)
      

  2.   

    API文档不是写的很详细了么,还要别的东西干什么?
      

  3.   

    我不太懂java的语法
    能不能详细的讲讲getMethod和invoke的语法,
    最好有实例
    被调用的函数有class MyClass为参数??
    多谢了!!!!
      

  4.   

    假设你的Automobile类,有三个同名但是签名不同的方法,
    load(Passengers p);
    load(Goods g);
    load(Driver d);Method [] methodsOfAutomobile = Class.forName("Automobile").getMethods();那么这三个Method对象,(即methodsOfAutomobile数组内包含的三个Method对象)对应了Automobile类里边的这三个方法,但注意不是等于。怎么样使用呢?Object dest_object = xxxxxx; //或者其他任何的对象;
    Object passenger = new Passenger(); //参数。
    methodsOfAutomobile[0].invoke(dest_object, passenger);
    ----------------------------------------------
    invoke
    public Object invoke(Object obj,
                         Object[] args)
                  throws IllegalAccessException,
                         IllegalArgumentException,
                         InvocationTargetExceptionInvokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary. 
    If the underlying method is static, then the specified obj argument is ignored. It may be null. If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null. If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, Second Edition, section 15.12.4.4; in particular, overriding based on the runtime type of the target object will occur. If the underlying method is static, the class that declared the method is initialized if it has not already been initialized. If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. If the underlying method return type is void, the invocation returns null. 
    Parameters:
    obj - the object the underlying method is invoked from
    args - the arguments used for the method call 
    Returns:
    the result of dispatching the method represented by this object on obj with parameters args 
    Throws: 
    IllegalAccessException - if this Method object enforces Java language access control and the underlying method is inaccessible. 
    IllegalArgumentException - if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion. 
    InvocationTargetException - if the underlying method throws an exception. 
    NullPointerException - if the specified object is null and the method is an instance method. 
    ExceptionInInitializerError - if the initialization provoked by this method fails.
    --------------------------------------------------
    呵呵,都可以进FAQ了,如果再不行。我也没辙了。不过强烈建议不要用这种走弯路的方法。现在既然在学Java,就不要用C++的思想去考虑问题。完全可以用 Java的思维来思考。