Java传递方法作为参数 [英] Java Pass Method as Parameter

查看:27
本文介绍了Java传递方法作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种通过引用传递方法的方法.我知道 Java 不会将方法作为参数传递,但是,我想获得替代方法.

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

我想要完成的事情与此类似:

public void setAllComponents(Component[] myComponentArray, Method myMethod) {for(组件叶子:myComponentArray){if (leaf instanceof Container) {//递归调用if Container容器节点=(容器)叶子;setAllComponents(node.getComponents(), myMethod);}//如果节点结束我的方法(叶);}//结束循环组件}

调用如:

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

解决方案

Edit:从 Java 8 开始,lambda 表达式 是一个不错的解决方案,如其他答案已经指出.下面的答案是为 Java 7 及更早版本编写的...

<小时>

查看命令模式.

//注意:代码未经测试,但我相信这是有效的 java...公共类命令示例{公共接口命令{公共无效执行(对象数据);}公共类 PrintCommand 实现命令{公共无效执行(对象数据){System.out.println(data.toString());}}public static void callCommand(命令命令,对象数据){命令.执行(数据);}公共静态无效主(字符串...参数){callCommand(new PrintCommand(), "hello world");}}

正如Pete Kirkham 指出的,还有另一种方法可以使用 Visitor.访问者方法稍微复杂一点 - 您的节点都需要使用 acceptVisitor() 方法来感知访问者 - 但如果您需要遍历更复杂的对象图,那么值得研究一下.>

I am looking for a way to pass a method by reference. I understand that Java does not pass methods as parameters, however, I would like to get an alternative.

I've been told interfaces are the alternative to passing methods as parameters but I don't understand how an interface can act as a method by reference. If I understand correctly an interface is simply an abstract set of methods that are not defined. I don't want to send an interface that needs to be defined every time because several different methods could call the same method with the same parameters.

What I would like to accomplish is something similar to this:

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());

解决方案

Edit: as of Java 8, lambda expressions are a nice solution as other answers have pointed out. The answer below was written for Java 7 and earlier...


Take a look at the command pattern.

// NOTE: code not tested, but I believe this is valid java...
public class CommandExample 
{
    public interface Command 
    {
        public void execute(Object data);
    }

    public class PrintCommand implements Command 
    {
        public void execute(Object data) 
        {
            System.out.println(data.toString());
        }    
    }

    public static void callCommand(Command command, Object data) 
    {
        command.execute(data);
    }

    public static void main(String... args) 
    {
        callCommand(new PrintCommand(), "hello world");
    }
}

Edit: as Pete Kirkham points out, there's another way of doing this using a Visitor. The visitor approach is a little more involved - your nodes all need to be visitor-aware with an acceptVisitor() method - but if you need to traverse a more complex object graph then it's worth examining.

这篇关于Java传递方法作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆