8

我正在阅读 Java 8 书,它附带了一个我复制的示例:

@FunctionalInterface
public interface Action {
    public void perform();
}

实施者:

public final class ActionImpl implements Action {
    public ActionImpl() {
        System.out.println("constructor[ActionIMPL]");
    }

    @Override
    public void perform() {
        System.out.println("perform method is called..");
    }    
}

来电者:

public final class MethodReferences {

    private final Action action;

    public MethodReferences(Action action) {
        this.action = action;
    }

    public void execute() {
        System.out.println("execute->called");
        action.perform();
        System.out.println("execute->exist");
    }

    public static void main(String[] args) {
        MethodReferences clazz = new MethodReferences(new ActionImpl());
        clazz.execute();
    }
}

如果调用此方法,则将以下内容打印到输出中:

constructor[ActionIMPL]
execute->called
perform method is called..
execute->exist

一切都很好,但是如果我使用方法引用而不是perform message打印方法!为什么会这样,我错过了什么吗?

如果我使用此代码:

MethodReferences clazz = new MethodReferences(() -> new ActionImpl());
clazz.execute();

或者这段代码:

final MethodReferences clazz = new MethodReferences(ActionImpl::new);

这是打印的:

execute->called
constructor[ActionIMPL]
execute->exist

不打印异常消息或其他任何内容。我正在使用 Java 8 1.8.25 64 位。

更新

对于像我一样正在学习的读者来说,这是正确的运行代码。

我创建了一个调用者类。

因为我需要实现一个空方法“从 Action 功能接口执行”,我需要将它作为参数传递给类构造函数MethodReference,所以我引用了“MethodReferenceCall 的构造函数,它是一个空构造函数”,我可以使用它。

public class MethodReferenceCall {
    public MethodReferenceCall() {
        System.out.println("MethodReferenceCall class constructor called");
    }

    public static void main(String[] args) {
        MethodReferenceCall clazz = new MethodReferenceCall();
        MethodReferences constructorCaller = new MethodReferences(MethodReferenceCall::new);
        constructorCaller.execute();
    }
}
4

1 回答 1

12

MethodReferences clazz = new MethodReferences(() -> new ActionImpl());

不使用方法引用,它使用 lambda 表达式。功能接口Action

public void perform();

所以

() -> new ActionImpl()

被翻译成类似的东西

new Action() {
    public void perform() {
        new ActionImpl();
    }
}

同样,在

MethodReferences clazz = new MethodReferences(ActionImpl::new);

theActionImpl::new确实使用了构造函数引用,它被翻译成类似

new Action() {
    public void perform() {
        new ActionImpl();
    }
}

ActionImpl::new不会调用new ActionImpl(). 它解析为预期类型的​​实例,其功能接口方法被实现为调用该构造函数。

于 2014-10-21T23:19:46.107 回答