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

解决方案 »

  1.   

    如果你的method2有返回值,并且和method1的参数类型一致,就可以method1(method2),这么写..
    但是在定义方法的时候应该不能把method2作为参数,没有具体了解过!
      

  2.   

    找到一个例子,但是没大看懂:
    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());
      

  3.   

    我很好奇Method的定义。也很想知道怎样做的好处是什么。求解!谢谢
      

  4.   


    http://www.coitweb.uncc.edu/~revesz/Fall2012/NewFix.html作业要求,不知道为啥
      

  5.   


    这个地方传的参数,第一个是个数组,第二个是个对象(对象名叫Method,不是你理解的method)
      

  6.   


    我们作业要求:
    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.http://www.coitweb.uncc.edu/~revesz/Fall2012/NewFix.html
      

  7.   

    是和java interface 有关吗?
      

  8.   

    Java没有函数指针,这东西很多时候很好用的。
      

  9.   

    用反射。public class DemoInvoke {
    public void sayHello() {
    System.out.println("hello java!");
    } public void foo(String methodName) {
    try {
    getClass().getDeclaredMethod(methodName).invoke(this);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    DemoInvoke di = new DemoInvoke();
    di.foo("sayHello");
    }}
      

  10.   


    public class DemoInvoke {
    public void sayHello() {
    System.out.println("hello java!");
    } public void foo(String methodName) {
    try {
    getClass().getDeclaredMethod(methodName).invoke(this);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    DemoInvoke di = new DemoInvoke();
    di.foo("sayHello");
    }}
      

  11.   

    把一个函数作为另一个函数的参数,这种功能称为闭包,目前的 JDK 还不支持这类语法,呵呵。