加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift等于__attribute((objc_requires_super))?

发布时间:2020-12-14 05:51:49 所属栏目:百科 来源:网络整理
导读:有Swift等于吗? __attribute((objc_requires_super)) 如果方法不称为超级方法,则会发出警告? 基本上,如果覆盖的方法不调用其超级方法,我想提醒(甚至更好的是抛出编译器错误)。 不,没有Swift等价于__attribute((objc_requires_super))。 等效功能,Swif
有Swift等于吗?
__attribute((objc_requires_super))

如果方法不称为超级方法,则会发出警告?

基本上,如果覆盖的方法不调用其超级方法,我想提醒(甚至更好的是抛出编译器错误)。

不,没有Swift等价于__attribute((objc_requires_super))。

等效功能,Swift Attributes,不包含此类属性。

Swift inheritance documentation的这个功能将被提及的部分只提到:

When you provide a method,property,or subscript override for a subclass,it is sometimes useful to use the existing superclass implementation as part of your override.

请注意,您可以使用final来防止覆盖函数,因此您可以通过提供由不可覆盖的方法调用的空的可覆盖方法来有效地完成所需的任务:

class AbstractStarship {
    var tractorBeamOn = false

    final func enableTractorBeam() {
        tractorBeamOn = true

        println("tractor beam successfully enabled")

        tractorBeamDidEnable()
    }

    func tractorBeamDidEnable() {
        // Empty default implementation
    }
}

class FancyStarship : AbstractStarship {
    var enableDiscoBall = false

    override func tractorBeamDidEnable() {
        super.tractorBeamDidEnable() // this line is irrelevant

        enableDiscoBall = true
    }
}

子类然后将覆盖可覆盖的方法,无论他们是否调用super还是不重要,因为超类的实现是空的。

正如Bryan Chen注释中的注释所示,如果子类被子类化,则会分解。

我没有声称这种做法在风格上是好的,但这当然是可能的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读