Swift等于__attribute((objc_requires_super))?
有Swift等于吗?
__attribute((objc_requires_super)) 如果方法不称为超级方法,则会发出警告? 基本上,如果覆盖的方法不调用其超级方法,我想提醒(甚至更好的是抛出编译器错误)。
不,没有Swift等价于__attribute((objc_requires_super))。
等效功能,Swift Attributes,不包含此类属性。 Swift inheritance documentation的这个功能将被提及的部分只提到:
请注意,您可以使用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注释中的注释所示,如果子类被子类化,则会分解。 我没有声称这种做法在风格上是好的,但这当然是可能的。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |