我正在寻找一种通过引用传递方法的方法。我知道Java不传递方法作为参数,但是,我想要一个替代方案。

我被告知接口是作为参数传递方法的替代方案,但我不理解接口如何通过引用充当方法。如果我理解正确的话,接口只是一组没有定义的抽象方法。我不想发送一个每次都需要定义的接口,因为几个不同的方法可以用相同的参数调用相同的方法。

我想要完成的是类似这样的事情:

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
}

调用方法如下:

setAllComponents(this.getComponents(), changeColor());
setAllComponents(this.getComponents(), changeSize());

当前回答

使用观察者模式(有时也称为监听者模式):

interface ComponentDelegate {
    void doSomething(Component component);
}

public void setAllComponents(Component[] myComponentArray, ComponentDelegate delegate) {
    // ...
    delegate.doSomething(leaf);
}

setAllComponents(this.getComponents(), new ComponentDelegate() {
                                            void doSomething(Component component) {
                                                changeColor(component); // or do directly what you want
                                            }
                                       });

新的ComponentDelegate()……声明实现接口的匿名类型。

其他回答

使用java.lang.reflect.Method对象并调用invoke

使用观察者模式(有时也称为监听者模式):

interface ComponentDelegate {
    void doSomething(Component component);
}

public void setAllComponents(Component[] myComponentArray, ComponentDelegate delegate) {
    // ...
    delegate.doSomething(leaf);
}

setAllComponents(this.getComponents(), new ComponentDelegate() {
                                            void doSomething(Component component) {
                                                changeColor(component); // or do directly what you want
                                            }
                                       });

新的ComponentDelegate()……声明实现接口的匿名类型。

自Java 8以来,有一个Function<T, R>接口(docs),它有方法

R apply(T t);

您可以使用它将函数作为参数传递给其他函数。T是函数的输入类型,R是返回类型。

在你的例子中,你需要传递一个函数,它接受Component类型作为输入,并且不返回任何东西——Void。在这种情况下,Function<T, R>不是最好的选择,因为没有Void类型的自动装箱。您正在寻找的接口名为Consumer<T> (docs) with method

void accept(T t);

它看起来是这样的:

public void setAllComponents(Component[] myComponentArray, Consumer<Component> myMethod) {
    for (Component leaf : myComponentArray) {
        if (leaf instanceof Container) { 
            Container node = (Container) leaf;
            setAllComponents(node.getComponents(), myMethod);
        } 
        myMethod.accept(leaf);
    } 
}

你可以使用方法引用来调用它:

setAllComponents(this.getComponents(), this::changeColor);
setAllComponents(this.getComponents(), this::changeSize); 

假设在同一个类中定义了changeColor()和changeSize()方法。


如果你的方法碰巧接受多个参数,你可以使用bifuncfunction <T, U, R> - T和U是输入参数的类型,R是返回类型。还有bicconsumer< T, U>(两个参数,没有返回类型)。不幸的是,对于3个或更多的输入参数,您必须自己创建一个接口。例如:

public interface Function4<A, B, C, D, R> {

    R apply(A a, B b, C c, D d);
}

虽然这还不适用于Java 7及以下版本,但我相信我们应该展望未来,至少认识到Java 8等新版本中会出现的变化。

也就是说,这个新版本为Java带来了lambda和方法引用(以及新的api,这是这个问题的另一个有效解决方案)。虽然它们仍然需要一个接口,但不会创建新的对象,并且额外的类文件不会因为JVM的不同处理而污染输出目录。

这两种风格(lambda和方法引用)都需要一个具有单一方法的可用接口,该方法的签名被使用:

public interface NewVersionTest{
    String returnAString(Object oIn, String str);
}

从这里开始,方法的名称不再重要。在接受lambda的地方,也接受方法引用。例如,在这里使用我们的签名:

public static void printOutput(NewVersionTest t, Object o, String s){
    System.out.println(t.returnAString(o, s));
}

这只是一个简单的接口调用,直到传递lambda1:

public static void main(String[] args){
    printOutput( (Object oIn, String sIn) -> {
        System.out.println("Lambda reached!");
        return "lambda return";
    }
    );
}

这将输出:

Lambda reached!
lambda return

方法引用类似。考虑到:

public class HelperClass{
    public static String testOtherSig(Object o, String s){
        return "real static method";
    }
}

和主要:

public static void main(String[] args){
    printOutput(HelperClass::testOtherSig);
}

输出将是真正的静态方法。方法引用可以是静态的、实例的、带有任意实例的非静态的,甚至是构造函数。对于构造函数,将使用类似ClassName::new的东西。

1有些人不认为这是lambda,因为它有副作用。不过,它确实以一种更直观的方式说明了如何使用。

我没有找到任何解决方案,在这里,显示如何传递方法与参数绑定到它作为一个方法的参数。下面的例子演示了如何传递一个已经绑定了参数值的方法。

步骤1:创建两个接口,一个有返回类型,另一个没有。Java也有类似的接口,但它们没有什么实际用途,因为它们不支持异常抛出。



    public interface Do {
    void run() throws Exception;
    }


    public interface Return {
        R run() throws Exception;
    }

我们如何使用两个接口在事务中封装方法调用的示例。注意,我们传递方法时带有实际参数。



    //example - when passed method does not return any value
    public void tx(final Do func) throws Exception {
        connectionScope.beginTransaction();
        try {
            func.run();
            connectionScope.commit();
        } catch (Exception e) {
            connectionScope.rollback();
            throw e;
        } finally {
            connectionScope.close();
        }
    }

    //Invoke code above by 
    tx(() -> api.delete(6));

另一个例子展示了如何传递一个实际返回值的方法



        public  R tx(final Return func) throws Exception {
    R r=null;
    connectionScope.beginTransaction();
    try {
                r=func.run();
                connectionScope.commit();
            } catch (Exception e) {
                connectionScope.rollback();
                throw e;
            } finally {
                connectionScope.close();
            }
        return r;
        }
        //Invoke code above by 
        Object x= tx(() -> api.get(id));