简体   繁体   中英

Kotlin lambdas without using objects

I am trying to convert from java to kotlin. The current java interface is something like this:

interface MyInterface {
    void foo(int x, int y);
}

MyInterface testing = (int a, int b) -> System.out.print("TESTING");

My current kotlin conversion is:

interface MyInterface {
    fun foo(x:Int, y:Int)
}

val kotlinConversion = object: MyInterface {
    override fun foo(x: Int, y: Int) {
        println("TESTING")
    }
}

Is there a way to write the variable kotlinConversion such that it is similar to the one in java without having to override the function?

没有覆盖你可以像这样直接使用的功能

var kotlinConversion = { a: Int, b: Int -> print("TESTING") }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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