当前位置:网站首页>Inheritance in swift
Inheritance in swift
2022-07-28 23:18:00 【Fluttering moth】
Inherit (Inheritance)
1、 Value type ( enumeration 、 Structure ) Inheritance is not supported , Only classes support inheritance
2、 A class without a parent , be called : Base class
Swift Did not like OC、Java That rule : Any class will eventually inherit from a base class .
3、 Subclasses can override the subscript of the parent class 、 Method 、 attribute , Rewrite must add override keyword .
Memory structure
class Animal {
var age = 0
}
class Dog : Animal {
var weight = 0
}
class ErHa : Dog {
var iq = 0
}let a = Animal()
a.age = 10to glance at a How much memory is needed ,a It's heap space , So it must be 16 Multiple , At the front 8 Bytes are used to put type information , the second 8 Bytes put reference count related things , Further back 8 Bytes is put age, A total of 24 Bytes , But the guarantee is 16 Multiple , So it is 32 Bytes .
Dog Because there is inheritance , So it's equivalent to
class Animal {
var age = 0
}
class Dog : Animal {
var weight = 0
}
class Dog {
var age = 0
var weight = 0
}
class ErHa : Dog {
var iq = 0
}let d = Dog()
d.age = 10
d.weight = 20d There are two properties in an object ,age and weight, Each account 8 Bytes , And generally speaking, the attribute memory of the parent class is higher ,d Objects also occupy 32 Bytes , The first is type related 8 Bytes , The second block is related to reference counting 8 Bytes , The third is storage age Of 8 Bytes , The fourth is storage weight Of 8 Bytes .
The same one ErHa Object must have 24 Byte store age、weight、iq, In addition, there is the front 16 Bytes , So it is 40 Bytes , But make sure it is 16 Multiple , So is 48.
Override instance methods 、 Subscript
class Animal {
func speak() {
print("Animal speak")
}
subscript(index: Int) -> Int {
return index
}
}
class Cat : Animal {
override func speak() {
super.speak()
print("Cat speak")
}
override subscript(index: Int) -> Int {
return super[index] + 1
}
}
var anim: Animal
anim = Animal()
//Animal speak
anim.speak()
//6
print(anim[6])
anim = Cat()
//Animal speak
//Cat speak
anim.speak()
// 7
print(anim[6])Override type methods 、 Subscript
1、 By class Decorated type method 、 Subscript , Allow to be overridden by subclasses
2、 By static Decorated type method 、 Subscript , It is not allowed to be overridden by subclasses
class Animal {
class func speak() {
print("Animal speak")
}
class subscript(index: Int) -> Int {
return index
}
}
class Cat : Animal {
override class func speak() {
super.speak()
print("Cat speak")
}
override class subscript(index: Int) -> Int {
return super[index] + 1
}
}
static Decorated type method 、 An error is reported when the subscript is rewritten

Subclass overrides can be used static modification , Just can't continue to be rewritten
Overridden properties
1、 Subclasses can attribute the parent class ( Storage 、 Calculation ) Rewrite to compute property
2、 Subclasses cannot override parent class attributes to storage attributes
3、 Can only override var attribute , Can not rewrite let attribute
4、 When rewriting , Property name 、 The type should be consistent
5、 Subclass overridden attribute permissions , Cannot be less than the permission of the parent attribute
If the parent property is read-only , Then the overridden attributes of subclasses can be read-only , It can also be readable and writable .
If the parent property is readable and writable , Then the properties of subclasses after rewriting must also be readable and writable .
Override instance properties
class Circle {
var radius: Int = 0
var diameter: Int {
set {
print("Circle setDiameter")
radius = newValue / 2
}
get {
print("Circle getDiameter")
return radius * 2
}
}
}
class SubCircle: Circle {
override var radius: Int {
set {
print("SubCircle setRadius")
super.radius = newValue > 0 ? newValue : 0
}
get {
print("SubCircle getRadius")
return super.radius
}
}
override var diameter: Int {
set {
print("SubCircle setDiameter")
super.diameter = newValue > 0 ? newValue : 0
}
get {
print("SubCircle getDiameter")
return super.diameter
}
}
}var circle = SubCircle()
circle.radius = 6
//SubCircle setRadius
print(circle.diameter)
//SubCircle getDiameter
//Circle getDiameter
//SubCircle getRadius
//12
circle.diameter = 20
//SubCircle setDiameter
//Circle setDiameter
//SubCircle setRadius
print(circle.radius)
//SubCircle getRadius
//10Override type properties
1、 By class Decorated calculation type attribute , Can be overridden by subclass
The storage type attribute can only be used static To modify .
2、 By static Decorated type attribute ( Storage 、 Calculation ), Can't be overridden by subclass
Attribute viewer
1、 It can be a parent class attribute in a subclass ( Except for read-only calculation properties 、let attribute ) Add attribute viewer
class Circle {
var radius: Int = 1
}
class SubCircle: Circle {
override var radius: Int {
willSet {
print("SubCircle willSetRadius", newValue)
}
didSet {
print("SubCircle didSetRadius", oldValue, radius)
}
}
}
var circle = SubCircle()
circle.radius = 10
//SubCircle willSetRadius 10
//SubCircle didSetRadius 1 102、 There are attribute observers in both parent and child classes
class Circle {
var radius: Int = 1 {
willSet {
print("Circle willSetRadius", newValue)
}
didSet {
print("Circle didSetRadius", oldValue, radius)
}
}
}
class SubCircle: Circle {
override var radius: Int {
willSet {
print("SubCircle willSetRadius", newValue)
}
didSet {
print("SubCircle didSetRadius", oldValue, radius)
}
}
}
var circle = SubCircle()
circle.radius = 10
//SubCircle willSetRadius 10
//Circle willSetRadius 10
//Circle didSetRadius 1 10
//SubCircle didSetRadius 1 103、 Subclasses can add attribute observers to the calculated attributes in the parent class .
class Circle {
class var radius: Int {
set {
print("Circle setRadius", newValue)
}
get {
print("Circle getRadius")
return 20
}
}
}
class SubCircle: Circle {
override static var radius: Int {
willSet {
print("SubCircle willSetRadius", newValue)
}
didSet {
print("SubCircle didSetRadius", oldValue, radius)
}
}
}
SubCircle.radius = 10
// Circle getRadius (oldValue)
// SubCircle willSetRadius 10
// Circle setRadius 10
// Circle getRadius (radius)
// SubCircle didSetRadius 20 20final
1、 By final The method of decoration 、 Subscript 、 attribute , Cannot be rewritten
2、 By final Modified class , Forbidden to be inherited
边栏推荐
- DirectX repair tool download (where is exagear simulator package)
- CGLIb 创建代理
- Terminal output G_ Debug() information
- Bullet frame mask layer "recommended collection"
- 18张图,直观理解神经网络、流形和拓扑
- Cnpm installation steps
- Submission records of frontiers Publishing House (with status changes)
- Typescript防止基类被实例化
- 安全狗入选《云安全全景图2.0》多个细项
- Applet, JS, transfer object jump transfer parameter problem
猜你喜欢

The industry's first cloud native security detection dual model! Safety dog heavyweight report appears at the digital China Construction Summit

Thesis reading (2) - vggnet of classification
![[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code](/img/31/e4cd4c261a7fc5cfa731976314530b.png)
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code

Console.log() console display... Solution

Target detection notes -yolo

c语言进阶篇:指针(三)
![[image segmentation] vein segmentation based on directional valley detection with matlab code](/img/82/7b7b761c975cd5c2f5b8f3e43592d2.png)
[image segmentation] vein segmentation based on directional valley detection with matlab code

Introduction to address book export without code development platform

【雷达】基于核聚类实现雷达信号在线分选附matlab代码

Several common methods of SQL optimization
随机推荐
leetcode 199. 二叉树的右视图
这个胶水有多强呢?
Summary of koltin knowledge points
Cnpm installation steps
一种分布式深度学习编程新范式:Global Tensor
MySQL foundation - advanced functions
Summary of common formula notes for solving problems in Higher Mathematics
Empowering Chinese core entrepreneurs! See how Moore elite solves the development problems of small and medium-sized chip Enterprises
Record a question about the order of trigonometric function exchange integrals
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
Improvement 18 of yolov5: the loss function is improved to alpha IOU loss function
Swift type attribute and its attentions
Invest 145billion euros! EU 17 countries announce joint development of semiconductor technology
Target detection notes SSD
Recurrent neural network (RNN)
【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码
Console.log() console display... Solution
PCA learning
[C language] implementation of three piece chess games
[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)
