比如把 method1 作为 method2 的参数. 可以吗? 如何弄?必须要是abstract method吗?谢谢

解决方案 »

  1.   


    那这个例子呢?:public void setAllComponents(Component[] myComponentArray, Method myMethod) {
        for (Component leaf : myComponentArray) {
            if (leaf instanceof Container) { //recursive call if Container
                Container node = (Container) leaf;
                setAllComponents(node.getComponents(), myMethod);
            } //end if node
            myMethod(leaf);
        } //end looping through components
    }
    invoked such as:setAllComponents(this.getComponents(), changeColor());
    setAllComponents(this.getComponents(), changeSize());
      

  2.   

    setAllComponents(this.getComponents(), changeColor());说明 changeColor() 的返回值为setAllComponents方法的第二个参数
      

  3.   

    我们作业要求:The purpose of this project is to demonstrate the technique for passing function names (i.e., method names) as parameters to other functions/methods in Java.Note that you should use abstract methods to provide functional, i.e. method types.http://www.coitweb.uncc.edu/~revesz/Fall2012/NewFix.html
      

  4.   

    可以通过反射去调用一个方法(方法名为String),遍历得到所有的Method,然后去判断方法名是否相同,相同就执行这个method
      

  5.   

    这个意思?import java.lang.reflect.InvocationTargetException;public class Invoker { public static Object invoke(Object o, String method, Class[] paramTypes, Object[] params) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
    return o.getClass().getDeclaredMethod(method, paramTypes).invoke(o, params);
    }

    public String testMethod(String s){
    System.out.println(s);
    return s + "return";
    }

    public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Invoker inv = new Invoker();
    String s = "toto";
    Class[] c = {s.getClass()};
    Object[] o = {s}; 
    System.out.println(Invoker.invoke(inv, "testMethod", c, o));
    }
    }
      

  6.   

    把方法名做为参数传给另一个方法,  然后通过反射去调用是可以的.
    或者可以传递一个Method对象过去..