【问题标题】:Swift class multiple inheritanceSwift 类多重继承
【发布时间】:2019-04-14 23:27:21
【问题描述】:

我创建了这样一个类:

class number1: UIScrollView {

init() {
    super.init(frame: CGRect(x: 9, y: 780, width: 1024, height: 267))
    self.contentSize = CGSize(width: 100, height: 267)
    self.backgroundColor = UIColor.clear
    self.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.RawValue(UInt8(UIView.AutoresizingMask.flexibleWidth.rawValue)))

    //followed by do blablabla I dont want in my new class

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

现在我想要一个类似于我的第一堂课但没有 blablabla 的第二堂课。

class number2: number1 {

override init() {
    super.init()
    self.frame = CGRect(x: 400, y: 10, width: 196, height: 500)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

我正在这样做,但不知道如何删除“blablabla”。最简单的方法是从 UIScrollView 而不是类 number1 继承 init。该怎么做?

【问题讨论】:

    标签: swift class multiple-inheritance


    【解决方案1】:

    这里只是简单的骨架,但应该足够了:

    class number1: UIScrollView {
        init() {
            super.init()
            // Do stuff common to all classes
            setup()
        }
    
        func setup() {
            // Do stuff for this class
        }
    }
    
    
    class number2: number1 {
        override func setup() {
            // Do stuff for this class only
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 2016-12-05
      • 2017-02-04
      相关资源
      最近更新 更多