https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203
superClass的initializers可选择继承
好处:
1.屏蔽一些功能
如: super:长方形 sub: 正方形
super中有长和宽的initializer,在sub中不应该有这个initializer
建议:
继承时先考虑要不要继承初始化方法
以下是默认全继承的条件:
保证了所有初始化方法都是安全的
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.
网友评论