I'm creating interface and some function in it has a body. It's required, that class that implements this interface must call super in overriding function before executing other code. How can I do this?

interface Watcher {
    fun funWithoutBody()

    fun startWatching() {
        //do some important stuff which must be called
    }
}

推荐答案

我意外地找到了我要找的东西.它是androidx.annotation包中提供的@CallSuper批注.Docs

使用@CallSuper注释验证重写方法

@CallSuper
protected fun onCreate(savedInstanceState: Bundle?) {
}

Kotlin相关问答推荐

如何在 Big Data 中使用Inc过滤器?

等待下一个值时暂停Kotlin Coroutine

计算值的Elvis算子

如何接受任何派生类KClass

Kotlin stlib中是否有用于将列表<;对<;A,B&>;转换为对<;列表<;A&>,列表<;B&>;的函数

为什么Kotlin不用static inner class来实现带有object关键字的单例呢?

如果带注释的成员未被特定块包围,则发出 IDE 警告

判断 Kotlin 变量是否为函数

如何在 Android Jetpack Compose 中的画布上绘制一侧加厚的描边?

使用空键映射获取

你怎么知道什么时候需要 yield()?

如何有效地填充 Gradle Kotlin DSL 中的额外属性?

什么是 .kotlin_builtins 文件,我可以从我的 uberjars 中省略它们吗?

未在IntelliJ IDEA上运行临时文件

当被Spring代理类访问时,Kotlin实例变量为null

类型不匹配推断类型为单位,但应为空

如何序列化/反序列化Kotlin密封类?

Kotlin Android:属性委托必须有一个 'getValue(DashViewModel, KProperty*>)' 方法

Lint 错误:可疑的相等判断:在 Object DiffUtilEquals 中未实现 equals()

比较Kotlin的NaN