任何人都可以告诉我为什么这不编译?
public class TestClass {
private boolean doThis = false;
protected void fooThat() {}
protected void fooThis() {}
public void execute() {
(doThis ? this::fooThis : this::fooThat).run();
}
}
Run Code Online (Sandbox Code Playgroud)
你的意图很可能是什么
(doThis ? this::fooThis : (Runnable) (this::fooThat)).run();
Run Code Online (Sandbox Code Playgroud)
Java无法单独从方法名称推断出您希望?:返回的类型.
我不确定这比这更好
if (doThis)
fooThis();
else
fooThat();
Run Code Online (Sandbox Code Playgroud)
这样做的方法如下:
Runnable r = (doThis ? this::fooThis : this::fooThat);
r.run();
Run Code Online (Sandbox Code Playgroud)
您的代码无法编译,因为:
run().| 归档时间: |
|
| 查看次数: |
1013 次 |
| 最近记录: |