[swift]初始化方法自动继承
2014-10-26 16:52
239 查看
子类默认不会继承父类的初始化方法,然而,如果某种条件满足的话,父类的初始化方法还是可以继承给子类。在通常情况下,这意味着你不必复写父类的初始化方法,在安全的前提下可以以最低的代价继承父类的初始化方法。
假设子类新增的stored properties 都提供了默认值,那么提供了以下两条规则:
规则1:
如果你的子类没有定义任何的指定初始化方法(新增便利初始化方法可有可无),那么子类会自动继承父类的所有指定初始化方法。
规则2:
如果子类通过规则1,或者通过自定义实现父类的所有指定初始化方法,那么子类自动继承父类的所有便利初始化方法。(子类以便利初始化方法覆盖父类的指定初始化方法也视为对父类的指定初始化方法的实现。)
【注:Swift 的class 类型的初始化方法包括两种类型,指定初始化方法(Designated Initializer)和便利初始化方法(Convenience Initializer),默认是指定初始化方法,一个class类型至少提供一个指定初始化方法。)
原文:
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.
假设子类新增的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 automaticallyinherited 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----方法 、 下标 、 继承 、 初始化 、 析构方法 、 可选链
- 13.8 Swift构造方法的自动继承
- Swift - 父子继承关系的初始化方法调用顺序
- Swift----方法 、 下标 、 继承 、 初始化 、 析构方法 、 可选链
- [swift]初始化方法自己主动继承
- Objective-C_继承_初始化方法
- OC3_继承和初始化方法
- Java学习记录(一):Java中继承与静态static、非静态、构造方法等的初始化顺序
- 【继承】指定初始化方法【便利构造器】
- 项目部署时自动初始化数据库的方法
- [Swift]Swift的初始化方法
- Android中实现自动生成布局View的初始化代码方法
- java 继承 方法重写 自动转型 多态 接口 抽象类
- 继承、初始化⽅方法、便利构造器
- 继承 初始化方法 便利构造器
- Objective-C( 继承,初始化方法)
- Swift 中的静态方法继承
- [Objective-C] 03.OC中的继承和初始化方法
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法
- OC之继承,初始化方法