scala中使用java_是否可以在Scala中使用Java 8样式方法引用?

我正在Scala中开发JavaFX8应用程序,但无法弄清楚如何将方法引用传递给事件处理程序。为了澄清,我没有使用ScalaFX库,而是直接在JavaFX之上构建我的应用程序。

这是相关的代码片段。

InputController.java (我用Java编写了此测试类,以隔离问题以仅使用方法参考)

public class InputController {

public void handleFileSelection(ActionEvent actionEvent){

//event handling code

}

public InputController() {

//init controller

}

}

这有效(Java)

InputController inputController = new InputController();

fileButton.setOnAction(inputController::handleFileSelection);

这不起作用(Scala)

val inputController = new InputController

fileButton.setOnAction(inputController::handleFileSelection)

这是来自编译器(Scala 2.11.6)的错误消息。

Error:(125, 45) missing arguments for method handleFileSelection in class Main;

follow this method with '_' if you want to treat it as a partially applied function

fileButton.setOnAction(inputController::handleFileSelection)

^

如果我改用Scala 2.12.0-M2,则会收到其他错误消息。

Error:(125, 45) missing argument list for method handleFileSelection in class Main

Unapplied methods are only converted to functions when a function type is expected.

You can make this conversion explicit by writing `handleFileSelection _` or `handleFileSelection(_)` instead of `handleFileSelection`.

fileButton.setOnAction(inputController::handleFileSelection)

^

Scala是否可以利用本机方式来利用Java

8中引入的方法引用?我知道使用lambda表达式的隐式转换方法,但是我想知道是否有一种方法可以使用类似于Java 8的方法引用,而无需使用lambda

decleration。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值