我正在创建界面,其中的一些功能有一个主体。
要求实现该接口(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/