美文网首页
Inheritance

Inheritance

作者: 夜雨聲煩_ | 来源:发表于2017-11-03 10:10 被阅读0次
  • 继承是swift中类区别于结构体和枚举等其他类型的主要原因。
  • 子类可以调用或者重写父类的属性、方法和附属脚本。可以通过属性监测观察所继承的属性。
  • 不继承其他类的类被称为基类。swift不默认继承某一基类(不同于OC中默认继承NSObject),如果自己不指定所继承类,那么它自己就是基类
  • 需要重载时使用override关键字来重载,避免意外重载发生错误
  • 可以使用super来读取父类的属性、方法以及附属脚本。
  • 重写属性时,可以通过重写时添加get和set来将父类中的只读改为读写,但是反过来,不可以将读写重写成只读(子类应用于父类的拓展而非限制)
  • 重写属性时,如果重写了set则一定需要重写get,如果不需要处理可以直接返回父类的属性(super.someProperty)
  • 重写属性监测器时不可以重写常量存储属性和只读计算属性,此二者不会被set,所以没有对应的willset和didset方法。同时,不可以在重写setter时加入属性监测器,如果你要加入,可以在setter中直接观察属性变化
  • 使用final关键字阻止子类重写,例如 final var, final func, final class func, 以及final subscript。甚至阻止产生子类,使用final class

相关文章

  • Inheritance

    继承是swift中类区别于结构体和枚举等其他类型的主要原因。 子类可以调用或者重写父类的属性、方法和附属脚本。可以...

  • Inheritance

    Overriding Overriding Property Getters and Setters We can...

  • 1-多态

    1. 继承-inheritance 表达了实现的复用。inheritance 是从代码复用角度提出的概念,和类型没...

  • Swift - Inheritance

    Inheritance 类能添加属性观察者到继承属性中,无论是存储还是计算属性 某个类没有继承于其他类,就是基类 ...

  • Inheritance Note

    Hypernym and Hyponym We use the word hyponym for the oppo...

  • 52 - Inheritance

  • model inheritance

    model的继承是和普通的python类继承相似的,只是最初的基类是应该从models.Model中派生出来 种类...

  • protected inheritance

    Accessibility of Derived-to-Base conversion in protected ...

  • django inheritance

    Abstract base classes 在元数据中设置Meta中设置 abstract=True ,这个mod...

  • class inheritance

    When a class inherits from another class.The inherited cl...

网友评论

      本文标题:Inheritance

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