gpt4 book ai didi

Java 8 - 三元运算符返回函数不编译

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:14:55 25 4
gpt4 key购买 nike

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

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

关于Java 8 - 三元运算符返回函数不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36769225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com