[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.
[swift]初始化方法自己主动继承的更多相关文章
- Swift - 初始化方法返回nil表示初始化失败
自Swift1.1开始,初始化init方法便有返回nil的能力.我们可以在init声明的时候在其后面加上一个 ? 或者 ! 来表示初始化失败时可能返回nil. 比如,给Int类添加一个将字符串初始化成 ...
- Swift的初始化方法
我们在深入初始化方法之前,不妨先再想想Swift中的初始化想要达到一种怎样的目的. 其实就是安全.在Objective-C中,init方法是非常不安全的:没有人能保证init只被调用一次,也没有人保证 ...
- Swift - 初始化Initialization
Ps:苹果官方文档-Initialization 自定义控件初始化中常见的几种错误(指定构造器和便利构造器)截图: 意思是:1.没有添加重写符override(重写父类方法)2.没有重写initW ...
- swift 初始化
知识点总结: 1.结构体 1.1.默认初始化方法:1.空初始化方法:2.成员初始化方法 struct Person{ var name = "tom" var age = } le ...
- Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)
原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...
- 5.Swift枚举|结构体|类|属性|方法|下标脚本|继承
1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum Celebrity{ case DongXie,XiDu,Nandi,BeiGai } // 从左 ...
- Swift枚举|结构体|类|属性|方法|下标脚本|继承
1. 枚举: ->在Swift中依然适用整数来标示枚举值,需搭配case关键字 enum Celebrity{ case DongXie,XiDu,Nandi,BeiGai } // 从左 ...
- Swift开发第十篇——可变参数函数&初始化方法顺序
本篇分为两部分: 一.Swift中的可变参数函数 二.初始化方法的顺序 一.Swift中的可变参数函数 可变参数函数指的是可以接受任意多个参数的函数,在 OC 中,拼接字符串的函数就属于可变参数函数 ...
- iOS学习之Object-C语言继承和初始化方法
一.继承 1.面向对象的三大特性:封装,继承,多态. 面向对象提供了继承特性.把公共的方法和实例变量写在父类里,子类只需要写自己独有的实例变量和方法即可.继承既能保证类的完整,又能简化代码. ...
随机推荐
- 运行top时,会报unknown terminal type错误
问题: 在板子上执行top时总是提示'vt102': unknown terminal type. 执行了export TERM=xterm,还是不管用 解决: 执行export,方向没有设置TERM ...
- uni - 条件渲染
vue官方文档和uni官方同步:https://cn.vuejs.org/v2/guide/conditional.html 1.多次切换建议使用v-show(始终保存在BOM) 2.因为if是惰性判 ...
- JSP实现数据保存(web基础学习笔记四)
session对象: //服务端设置Session属性 session.setAttribute("user", username); //客户端接收Session Object ...
- Ubuntu 源码方式安装Subversion
使用到的安装包: apr-1.5.1.tar.gz apr-util-1.5.3.tar.gz pcre-8.35.tar.gz httpd-2.4.9.tar.bz2 subversion-1.8. ...
- 1z0-052 q209_4
4: You have two tables with referential integrity enforced between them. You need to insert data to ...
- eclipse could not create the Java Vitual Machine
eclipse could not create the Java Vitual Machine CreateTime--2018年4月27日11:07:15 Author:Marydon 1.情 ...
- Python之L.reverse()和L.sort()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之L.reverse()和L.sort() #http://python.jobbole.com ...
- 分离Command
要点: 1.请求类必须继承CommandBase 2.请求类类名为请求对象中的Key值,大小写可以不区分 3.类必须用public修饰,否则无法识别该请求,提示为无效请求 4.不能再调用NewRequ ...
- python第三方包的windows安装文件exe格式
今天弄了一上午的python-ldap,发现要么安装vc,要么用其他比较麻烦的方法,都比较麻烦.幸好找到这个地址: http://www.lfd.uci.edu/~gohlke/pythonlibs/ ...
- eclipse cdt Program "make" not found in PATH
eclipse cdt插件,开发c/c++程序,编译时报错Program "make" not found in PATH经查C:\MinGW\bin下确实无make.exe,有m ...