谁能告诉我为什么它不能编译?

public class TestClass {

    private boolean doThis = false;

    protected void fooThat() {}

    protected void fooThis() {}

    public void execute() {
        (doThis ? this::fooThis : this::fooThat).run();
    }
}

最佳答案

您的意图可能是

(doThis ? this::fooThis : (Runnable) (this::fooThat)).run();

Java不能仅从方法名称中推断出您期望?:返回的类型。

我不确定这是否比
if (doThis)
    fooThis();
else
    fooThat();

07-27 13:42