我想像这样传递一个接口作为参数:
class Test {
fun main() {
test({})
// how can I pass here?
}
fun test(handler: Handler) {
// do something
}
interface Handler {
fun onCompleted()
}
}
在 Java 中,我可以使用像 test(new Handler() { .......... })
这样的匿名函数,但我不能在 Kotlin 中这样做。有人知道怎么做吗?
原文由 maphongba008 发布,翻译遵循 CC BY-SA 4.0 许可协议
在科特林你可以这样做:
或者以同样的方式创建属性:
并且,在代码的某处:test(handler)