美文网首页
Swift-继承(Inheritance)

Swift-继承(Inheritance)

作者: 懒床小番茄 | 来源:发表于2020-07-07 10:33 被阅读0次

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

  • 值类型(枚举、结构体)不支持继承,只有类支持继承
  • 没有父类的类,称为:基类(Swift并没有像OC、Java那样的规定:任何类最终都要继承自某个基类)
  • 子类可以重写父类的下标、方法、属性,重写必须加上\color{red}{override}关键字
  • \color{red}{final}修饰的方法、下标、属性,禁止被重写
  • \color{red}{final}修饰的类,禁止被继承

重写实例方法、下标:

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[ ]。

重写类型方法、下标:

  • \color{red}{class}修饰的类型方法、下标,\color{red}{允许}被子类重写
  • \color{red}{static}修饰的类型方法、下标,\color{red}{不允许}被子类重写
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

重写属性:

注意细节:
  • 子类可以将父类的属性(存储、计算)重写为计算属性
  • 子类不可以将父类属性重写为存储属性
  • 只能重写var属性,不能重写let属性
  • 重写时,属性名、类型要一致
  • 子类重写后的属性权限 不能小于 父类属性的权限
  • 如果父类属性是只读的,那么子类重写后的属性可以是只读的、也可以是可读写的
  • 如果父类属性是可读写的,那么子类重写后的属性也必须是可读写的
  • 被class修饰的计算类型属性,可以被子类重写
  • 被static修饰的类型属性(存储、计算),不可以被子类重写

属性观察器:

  • 可以在子类中为父类属性(除了只读计算属性、let属性)增加属性观察器
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个字节。

相关文章

  • Swift-继承(Inheritance)

    在Swift中 ,类可以调用和访问超类的方法,属性和下标脚本,并且可以重写这些方法,属性和下标脚本来优化或修改它们...

  • Inheritance (继承)

    A class caninheritmethods, properties, and other characte...

  • 继承(Inheritance)

    值类型(枚举 结构体)不支持继承,只有类支持继承 没有父类的类,称为:基类 Swift没有像OC,Java那样的规...

  • 继承-Inheritance

    继承是面向对象编程的核心概念之一,它允许一个对象得到另一个对象的属性和方法,这使得削减代码和重用代码都变得简单。我...

  • 继承(Inheritance)

    当子类继承基类时,子类包含了父基类所有数据及操作的定义。 在 C++ 实践中,继承主要用于两种场合: 实现继承:子...

  • 虚函数

    虚函数 Inheritance (继承) with virtual functions (虚函数) 非虚函数:继承...

  • kotlin inheritance 继承

    Any 所有类都继承该类,提供默认的三个函数: equals()hashCode()toString()继承声明:...

  • Swift - 继承(Inheritance)

    继承(Inheritance) 值类型(枚举、结构体)不支持继承,只有 类 支持继承 基类:没有父类的类Swift...

  • 13 Inheritance 继承

    一个类可以从另一个类继承方法、属性和其他特征。当一个类从另一个类继承时,继承类称为子类,它继承的类称为超类。继承是...

  • C++中类的组合(BOOLAN教育)

    Inheritance(继承),Composition(复合),Delegation(委托) Compositio...

网友评论

      本文标题:Swift-继承(Inheritance)

      本文链接:https://www.haomeiwen.com/subject/imooqktx.html