android - 注解必须调用super的接口(interface)函数

标签 android kotlin annotations

我正在创建界面,其中的一些功能有一个主体。
要求实现该接口(interface)的类在执行其他代码之前必须在覆盖函数中调用super。
我怎样才能做到这一点?

interface Watcher {
    fun funWithoutBody()

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

最佳答案

我无意中找到了我正在寻找的东西。这是一个 @CallSuper androidx.annotation 中可用的注释包裹。 Docs

Use the @CallSuper annotation to validate that an overriding method calls the super implementation of the method. The following example annotates the onCreate() method to ensure that any overriding method implementations call super.onCreate():

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

关于android - 注解必须调用super的接口(interface)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40152959/

相关文章:

android - 在数据 fragment 之间传递数据

java - Java注解可以在代码中插入断点吗

spring - 在缓存中找不到但不缓存结果时,如何使@Cacheable 返回null?

android - 无法下载 kotlin-reflect.jar (org.jetbrains.kotlin :kotlin-reflect:1. 3.0):没有可用于离线模式的缓存版本

java - 尝试从在某些标签之间读取的缓冲读取器中提取子字符串

安卓系统应用程序

android - 禁用耳机麦克风并启用设备麦克风。

kotlin - Kotlin分配的意外值(value)

spring - 在自定义注释的方面传递方法参数

java - Android SDK r18 似乎破坏了 ORMLite