请教,把这段 Java 的代码转为 Kotlin 的

EDDGA 2022-05-10 11:33:56

如题。看起来是Java lumbda,可是 idea 转 kotlin 和自己转都很多错误,请教正确的转换写法:

public interface SheepShearCallback {
 
    Event<SheepShearCallback> EVENT = EventFactory.createArrayBacked(SheepShearCallback.class,
        (listeners) -> (player, sheep) -> {
            for (SheepShearCallback listener : listeners) {
                ActionResult result = listener.interact(player, sheep);
 
                if(result != ActionResult.PASS) {
                    return result;
                }
            }
 
        return ActionResult.PASS;
    });
 
    ActionResult interact(PlayerEntity player, SheepEntity sheep);
}

 

...全文
160 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wresource 版主 2022-05-15
  • 打赏
  • 举报
回复

首先看看相关包是否导入成功,这边转换之后发现没有相关的包,语法上应该idea的转换没有多大问题

interface SheepShearCallback {
        fun interact(player: PlayerEntity?, sheep: SheepEntity?): ActionResult

        companion object {
            val EVENT: Event<SheepShearCallback> = EventFactory.createArrayBacked(
                SheepShearCallback::class.java,
                { listeners ->
                    label@{ player, sheep ->
                        for (listener in listeners) {
                            val result: ActionResult = listener.interact(player, sheep)
                            if (result !== ActionResult.PASS) {
                                return@label result
                            }
                        }
                        ActionResult.PASS
                    }
                })
        }
    }

3,281

社区成员

发帖
与我相关
我的任务
社区描述
Kotlin语言的技术分享社区,分享用Kotlin作为主流语言学习安卓过程的点点滴滴,一起学习才有动力
社区管理员
  • wresource
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

大家可以发一些用kotlin开发过程中的问题,多多学习和交流。

试试用AI创作助手写篇文章吧