Android kotlin覆盖onCreateView()方法内部的接口方法

Pin*_*jee 3 java android kotlin kotlin-extension

我是Kotlin的新手.我有一个包含两个方法定义的接口:

fun onSuccess(result: T)
fun onFailure(e: Exception)
Run Code Online (Sandbox Code Playgroud)

现在,在我的片段中,我实现了这个接口,并希望在里面使用这些方法:

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
     ..................
     ..................
     override fun onSuccess(result: String) {}
     override fun onFailure(e: Exception) {}
}
Run Code Online (Sandbox Code Playgroud)

在java中我们可以使用@override但在这里我得到错误'修饰符'覆盖'不适用于本地函数'.我在kotlin工作了2-3天,我喜欢它.但有些时候小问题需要一些时间来调试.

Dom*_*Dom 7

您需要在片段上实现接口,并将重写方法移到onCreateView方法之外.

class MyFragment:Fragment,MyInterface

您不能覆盖方法内的方法.另一个选项是您可以创建下面演示的对象表达式

window.addMouseListener(object : MouseAdapter() {
    override fun mouseClicked(e: MouseEvent) {
        // ...
    }

    override fun mouseEntered(e: MouseEvent) {
        // ...
    }
})
Run Code Online (Sandbox Code Playgroud)

https://kotlinlang.org/docs/reference/object-declarations.html