Pass interface as a parameter in Kotlin

I want to pass an interface as a parameter:

class Test { fun main() { test({}) // how can I pass here? } fun test(handler: Handler) { // do something } interface Handler { fun onCompleted() } } 

In Java, I can use an anonymous function like test(new Handler() { .......... }) , but I cannot do this in Kotlin. Does anyone know how to do this?

+7
android lambda interface kotlin
source share
1 answer

In Kotlin you can do:

 test(object: Handler { override fun onComplete() { } }) 

Or make the property the same way:

 val handler = object: Handler { override fun onComplete() { } } 

And somewhere in the code: test (handler)

+14
source share

All Articles