Joc*_*Doe 23 java methods lambda interface kotlin
什么是相当于kotlin中的代码,似乎没有什么工作我尝试:
public interface AnInterface {
void doSmth(MyClass inst, int num);
}
Run Code Online (Sandbox Code Playgroud)
在里面:
AnInterface impl = (inst, num) -> {
//...
}
Run Code Online (Sandbox Code Playgroud)
s1m*_*nw1 20
如果AnInterface是Java,您可以使用SAM转换:
val impl = AnInterface { inst, num ->
//...
}
Run Code Online (Sandbox Code Playgroud)
否则,如果界面是Kotlin ......
interface AnInterface {
fun doSmth(inst: MyClass, num: Int)
}
Run Code Online (Sandbox Code Playgroud)
...您可以使用object语法匿名实现它:
val impl = object : AnInterface {
override fun doSmth(inst:, num: Int) {
//...
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*nik 15
如果您正在将接口及其实现重写为Kotlin,那么您应该删除接口并使用功能类型:
val impl: (MyClass, Int) -> Unit = { inst, num -> ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6142 次 |
| 最近记录: |