gpt4 book ai didi

swift - 为什么我的父类(super class)在 Swift 中调用子类的方法而不是它自己的方法?

转载 作者:搜寻专家 更新时间:2023-11-01 07:30:01 25 4
gpt4 key购买 nike

这是一个示例项目:http://cl.ly/3N2u2i1S441M

我在 UITableViewCell 父类(super class)中,因为在启动子类时我调用了 super.init()。在子类和父类(super class)的 init 底部,我调用了一个方法,调用 styleCell 对其应用样式。此方法来自它们都遵守的协议(protocol),其中一个隐式遵守,因为它是子类并且它覆盖了该方法。

在父类(super class)的init 结束时,该样式方法被调用,但它调用子类 styleCell 方法,而不是它自己的。

到底为什么会这样?

这是 Swift 的错误吗?除了上面的项目之外,我还附加了一些代码来显示问题:

父类(super class)表格单元格:

class SuperTableViewCell: UITableViewCell, Style {
var mysuperview: UIView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

mysuperview = UIView()

doStyle()
}

required init?(coder aDecoder: NSCoder) {
fatalError("Must be created in code.")
}

func doStyle() {
print("super class")
}
}

子类表格单元格:

class SubTableViewCell: SuperTableViewCell {
var mysubview: UIView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

mysubview = UIView()

doStyle()
}

required init?(coder aDecoder: NSCoder) {
fatalError("Must be created in code.")
}

override func doStyle() {
super.doStyle()

mysubview!.backgroundColor = UIColor.greenColor()
}
}

样式类和协议(protocol):

class StyleManager: NSObject {

}

protocol Style {
func doStyle()
}

这会导致运行时错误,当子类单元格试图在 doStyle() 中设置其 View 时发生崩溃。

最佳答案

考虑了很多,我认为最好的说明是将所有内容都放在一个“类”中。

通过覆盖 doStyle 函数,您可以有效地替换它。您仍然可以使用 super. 访问它,但我们没有办法调用 super.super class 并让它指向它自己的方法。这也是有道理的,因为父类(super class)和子类不是两个对象。它们是一个对象,实际上具有一个代码源,其中一段在 super 中,另一段在 sub 中。

因此,当 doStyle 函数被 super init 触发时,它会看到被替换/覆盖的方法。

override 非常擅长做它的名字,覆盖方法。这几乎暗示您不会同时使用 super.method 和 override 方法。

除此之外,这也是不好的做法。为独特的功能使用独特的名称(这是你所拥有的)。在某种程度上,使用相同的名称/协议(protocol)是有意义的,因为在每个函数中你都在设置一个样式。但这不是两个都需要样式的独立类。您实际上是在设置基本样式和特定样式。两件不同的事情都需要完成。

class SuperSubIllustation {
// super and sub class as one for illustation

init() {
subInit()
}


func superInit() {
print("super init")

overrideDoStyle() // this calls the sub style because it's own method is overridden

}

func subInit() { // the sub init, this overrides the superInit
print("sub init")

superInit() // the super.init()

overrideDoStyle() // the sub style

}

func doStyle() {
print("super style")
}

func overrideDoStyle() {
print("sub style")

doStyle() // the super.doStyle

}
}

边注:

创建子类时,您通常会创建越来越具体的代码。例如一系列 UIButton 类。

class RoundedCornersButton : UIButton {

override init(frame: CGRect) {
super.init(frame: frame)
self.layer.cornerRadius = 5
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.layer.cornerRadius = 5
}
}


class GreenButton : RoundedCornersButton {

override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.greenColor()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.greenColor()
}
}

class RedButton : RoundedCornersButton {

override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.redColor()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.redColor()
}
}

即使 initsuper.init 都被调用了,也没有冲突,因为子类不会同时调用它自己的 init 和它是 super 的 init

关于swift - 为什么我的父类(super class)在 Swift 中调用子类的方法而不是它自己的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com