Swift-继承(Inheritance)

2020-07-07  本文已影响0人  懒床小番茄

在Swift中 ,类可以调用和访问超类的方法,属性和下标脚本,并且可以重写这些方法,属性和下标脚本来优化或修改它们的行为。Swift会检查你的重写定义在超类中是否有匹配的定义,保证重写的正确性。可以为类继承来的任何属性添加属性观察器,属性改变会被通知。

重写实例方法、下标:

class Animal {
    func speak() {
        print("Animal speak")
    }
    subscript(index: Int) -> Int {
        return index
    }
}
class Cat : Animal {
    override func speak() {
        super.speak() //调用父类的speak方法
        print("Cat speak")
    }
    override subscript(index: Int) -> Int {
        return super[index] + 1 //super[index]调用父类的下标
    }
}
var anim: Animal
anim = Cat()
// Animal speak
// Cat speak
anim.speak()
// 7
print(anim[6])

重写必须加上override关键字,调用父类的方法必须要加上super. ,调用父类的下标要用super[ ]。

重写类型方法、下标:

class Animal {
    class func speak() {
        print("Animal speak")
    }
    class subscript(index: Int) -> Int {
        return index
    }
}
class Cat : Animal {
    override class func speak() {
        super.speak()
        print("Cat speak")
    }
    override class subscript(index: Int) -> Int {
        return super[index] + 1
    }
}

如果将父类的class修饰词换成static,则会编译报错:Cannot override static method

重写属性:

注意细节:

属性观察器:

class Circle {
    var radius: Int = 1
}
class SubCircle : Circle {
    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}
var circle = SubCircle()
// SubCircle willSetRadius 10
// SubCircle didSetRadius 1 10
circle.radius = 10

子类可以重写父类的属性观察器

  class Circle {
    var radius: Int = 1 {
        willSet {
            print("Circle willSetRadius", newValue)
        }
        didSet {
            print("Circle didSetRadius", oldValue, radius) }
    } }
  class SubCircle : Circle {
    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius) }
    } }
  var circle = SubCircle()
  // SubCircle willSetRadius 10
  // Circle willSetRadius 10
  // Circle didSetRadius 1 10
  // SubCircle didSetRadius 1 10
  circle.radius = 10

子类可以为父类的计算属性添加属性观察器,我们知道在属性中,set和willSet、didSet是不能共存的,使用继承可以为计算属性添加属性观察器。

  class Circle {
    var radius: Int {
        set {
            print("Circle setRadius", newValue)
        }
        get {
            print("Circle getRadius")
            return 20 }
    } }
  class SubCircle : Circle {
    override var radius: Int {
        willSet {
            print("SubCircle willSetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius) }
    } }
  var circle = SubCircle()
  // Circle getRadius
  // SubCircle willSetRadius 10
  // Circle setRadius 10
  // Circle getRadius
  // SubCircle didSetRadius 20 20
  circle.radius = 10
  

内存结构分析:

首先定义一个父类Animal,然后定义子类Dog继承自Animal,ErHa继承自Dog

class Animal {
    var age = 0
}
class Dog : Animal {
    var weight = 0
}
class ErHa : Dog {
    var iq = 0
}

定义一个Animal类的对象a,然后为age进行赋值

let a = Animal()
a.age = 10
// 32
print(Mems.size(ofRef: a))
/*
 0x00000001000073e0
 0x0000000000000002
 0x000000000000000a
 0x0000000000000000
 */
print(Mems.memStr(ofRef: a))

Animal是存储于堆空间的对象,堆空间的内存必然是16的倍数,首先有8的字节放类型信息,然后有8个字节存储引用计数相关的信息,再然后才是有8个字节存储age,这样加起来一共24个字节,但是需要16的倍数,所以一个Animal对象32个字节。

定义一个Dog类的对象d

let d = Dog()
d.age = 10
d.weight = 20
// 32
print(Mems.size(ofRef: d))
/*
 0x0000000100007490
 0x0000000000000002
 0x000000000000000a
 0x0000000000000014
 */
print(Mems.memStr(ofRef: d))

相同前16个字节存放类型信息和引用计数相关的东西,然后8个字节存放从父类继承过来的age,再8个字节存放自己的weight(一般,从父类继承过来的放在前面),所以同样用了32个字节

定义一个ErHa类的对象e

let e = ErHa()
e.age = 10
e.weight = 20
e.iq = 30
// 48
print(Mems.size(ofRef: e)) /*
 0x0000000100007560
 0x0000000000000002
 0x000000000000000a
 0x0000000000000014
 0x000000000000001e
 0x0000000000000000
 */
print(Mems.memStr(ofRef: e))

因为ErHa继承自Dog,而Dog继承自Animal,所以需要24个字节来存储age、weight、iq,相同前16个字节存放类型信息和引用计数相关的东西,共用到了40个字节,但是需要是16的倍数,所以分配的是48个字节。

上一篇 下一篇

猜你喜欢

热点阅读