zoukankan      html  css  js  c++  java
  • [swift]初始化方法自己主动继承

        子类默认不会继承父类的初始化方法,然而,假设某种条件满足的话。父类的初始化方法还是能够继承给子类。在通常情况下,这意味着你不必复写父类的初始化方法。在安全的前提下能够以最低的代价继承父类的初始化方法。

        如果子类新增的stored properties 都提供了默认值。那么提供了下面两条规则:

    规则1:

    假设你的子类未定义不论什么的指定初始化方法(新增便利初始化方法可有可无)。那么子类会自己主动继承父类的全部指定初始化方法。

    规则2:

    假设子类通过规则1。或者通过自己定义实现父类的全部指定初始化方法,那么子类自己主动继承父类的全部便利初始化方法。(子类以便利初始化方法覆盖父类的指定初始化方法也视为对父类的指定初始化方法的实现。

    【注:Swift 的class 类型的初始化方法包含两种类型。指定初始化方法(Designated Initializer)和便利初始化方法(Convenience Initializer)。默认是指定初始化方法。一个class类型至少提供一个指定初始化方法。)


    原文:

    Automatic Initializer Inheritance

    As mentioned above, subclasses do not inherit their superclass initializers by default. However, superclass initializers are automatically inherited if certain conditions are met. In practice, this means that you do not need to write initializer overrides in many common scenarios, and can inherit your superclass initializers with minimal effort whenever it is safe to do so.

    Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:

    Rule 1

    If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

    Rule 2

    If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.

    These rules apply even if your subclass adds further convenience initializers.

    NOTE

    A subclass can implement a superclass designated initializer as a subclass convenience initializer as part of satisfying rule 2.



  • 相关阅读:
    第一次冲次(补)
    软件工程概论个人总结
    第16周进度条
    读《梦断代码》第2章有感
    java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。参数 1 (""): 数据类型 0x38 未知。
    怎么卸载VS2005呢?控制面板里内容太多,找不出哪些是属于VS2005?我的VS2005出问题了
    常用正则表达式的写法
    Http响应头字段详解,MyEclipse配置tomcat,servlet运行方式
    html框架 字体颜色 列表 表格 图片 定义列表 a标签
  • 原文地址:https://www.cnblogs.com/llguanli/p/6748825.html
Copyright © 2011-2022 走看看