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

如果子类新增的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.


[swift]初始化方法自己主动继承的更多相关文章

  1. Swift - 初始化方法返回nil表示初始化失败

    自Swift1.1开始,初始化init方法便有返回nil的能力.我们可以在init声明的时候在其后面加上一个 ? 或者 ! 来表示初始化失败时可能返回nil. 比如,给Int类添加一个将字符串初始化成 ...

  2. Swift的初始化方法

    我们在深入初始化方法之前,不妨先再想想Swift中的初始化想要达到一种怎样的目的. 其实就是安全.在Objective-C中,init方法是非常不安全的:没有人能保证init只被调用一次,也没有人保证 ...

  3. Swift - 初始化Initialization

    Ps:苹果官方文档-Initialization 自定义控件初始化中常见的几种错误(指定构造器和便利构造器)截图:   意思是:1.没有添加重写符override(重写父类方法)2.没有重写initW ...

  4. swift 初始化

    知识点总结: 1.结构体 1.1.默认初始化方法:1.空初始化方法:2.成员初始化方法 struct Person{ var name = "tom" var age = } le ...

  5. Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)

    原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...

  6. 5.Swift枚举|结构体|类|属性|方法|下标脚本|继承

    1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum  Celebrity{  case DongXie,XiDu,Nandi,BeiGai }  // 从左 ...

  7. Swift枚举|结构体|类|属性|方法|下标脚本|继承

    1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum  Celebrity{  case DongXie,XiDu,Nandi,BeiGai }  // 从左 ...

  8. Swift开发第十篇——可变参数函数&初始化方法顺序

    本篇分为两部分: 一.Swift中的可变参数函数 二.初始化方法的顺序 一.Swift中的可变参数函数 可变参数函数指的是可以接受任意多个参数的函数,在 OC 中,拼接字符串的函数就属于可变参数函数 ...

  9. iOS学习之Object-C语言继承和初始化方法

    一.继承 1.面向对象的三大特性:封装,继承,多态.      面向对象提供了继承特性.把公共的方法和实例变量写在父类里,子类只需要写自己独有的实例变量和方法即可.继承既能保证类的完整,又能简化代码. ...

随机推荐

  1. bash參考手冊之六(Bash特性)

    6 Bash 特性 这部分描写叙述Bash独有的特性. *  调用Bash : Bash能够接受的命令行选项. *  Bash启动文件 : Bash何时及怎样运行脚本. *  交互Shell : 什么 ...

  2. android studio中使用adb wifi插件无线调试程序

    使用android studio中使用adb wifi插件无线调试程序的前提条件电脑和手机在同一个无线网 1.下载adb wifi插件 File->Settings->Plugins Br ...

  3. UVA 10700 Camel trading 无括号的表达式 贪心

    题意:给出只包含数字和+*的表达式,你可以自己安排每一个运算的顺序,让你找出表达式可能得到的最大值和最小值. 很明显,先乘后加是最小值,先加后乘能得到最大值. 其实不是很明显... 证明下: 数字的范 ...

  4. The connection to adb is down, and a severe error has occured. 错误

    错误:The connection to adb is down, and a severe error has occured. [2010-03-11 09:36:56 - HelloOPone] ...

  5. Struts2(三)更改字符编码

    一.导入包和struts配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE stru ...

  6. Linux库的创建和使用

    Linux库的概念 库是一种软件组建技术,里面封装了数据和函数,提供给用户程序调用.使用库能够使程序模块化,提高编译速度,实现代码重用,易于升级. Windows系统提供了大量静态链接库(.lib)和 ...

  7. Js判断出生年月填写的 是否正确

    var r = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29 ...

  8. 解决 $ npm install node-sass --save-dev 失败的问题

    $ npm install --save node-sass --registry=https://registry.npm.taobao.org --disturl=https://npm.taob ...

  9. html-webpack-plugin插件 根据模板生成多页面

    1.项目目录结构为: 2.webpack.config.js配置文件为: var htmlWebpackPlugin = require('html-webpack-plugin'); module. ...

  10. Jsp和session、request.getSession()

    request.getSession(false); 这段代码代表,如果没有和当前request关联的session则不创建session并且返回空 request.getSession(true); ...