简体   繁体   English

Java Lambda方法引用不起作用

[英]Java Lambda method reference not working

My original code is this: 我的原始代码是这样的:

private static void onClicked(MouseEvent event) {
    // code to execute
}

// somewhere else in the program:
setOnMouseClicked(event -> SomeClass.onClicked(event));

But IntelliJ says "Can be replaced with method reference" which I'm not too sure how to do. 但IntelliJ说“可以用方法参考替换”,我不太清楚该怎么做。 I thought I would do this: 我以为我会这样做:

setOnMouseClicked(event -> SomeClass::onClicked);

But then that tells me "void is not a functional interface", but I don't want to return anything. 但后来告诉我“虚空不是一个功能界面”,但我不想返回任何东西。 I just want the handler to execute. 我只想要执行处理程序。 How can I fix this? 我怎样才能解决这个问题?

Thank you! 谢谢!

You are mixing a lambda expression with a method reference. 您正在将lambda表达式与方法引用混合使用。

Change 更改

setOnMouseClicked(event -> SomeClass::onClicked);

to

setOnMouseClicked(SomeClass::onClicked);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM