注释必须调用超级的接口函数 [英] Annotate interface function that must call super

查看:62
本文介绍了注释必须调用超级的接口函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建接口,并且其中的某些功能具有主体. 要求,实现此接口的类必须在执行其他代码之前在覆盖函数中调用super. 我该怎么办?

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
    }
}

推荐答案

我偶然发现了我要找的东西.这是android.support.annotation包中可用的@CallSuper批注. 文档

I've accidentally found, what I was looking for. It's a @CallSuper annotation available in android.support.annotation package. Docs

使用@CallSuper批注来验证该覆盖方法 调用该方法的超级实现.下面的例子 注释onCreate()方法以确保任何覆盖方法 实现调用super.onCreate():

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?) {
}

这篇关于注释必须调用超级的接口函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆