当前位置:网站首页>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
边栏推荐
- 18 diagrams, intuitive understanding of neural networks, manifolds and topologies
- 定了!哪吒S全系产品将于7月31日上市发售
- Improvement 18 of yolov5: the loss function is improved to alpha IOU loss function
- [radar] radar signal online sorting based on kernel clustering with matlab code
- 【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
- [physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code
- DirectX repair tool download (where is exagear simulator package)
- View APK signature
- 【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
- Summary of koltin knowledge points
猜你喜欢

MySQL数据库的基本概念以及MySQL8.0版本的部署(一)

18 diagrams, intuitive understanding of neural networks, manifolds and topologies

Applet, JS, transfer object jump transfer parameter problem

【C语言】三子棋小游戏实现

can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to host memory

Several common methods of SQL optimization

Console.log() console display... Solution

6 open source tutorials of super conscience!

Retrofit Usage Summary

cnpm安装步骤
随机推荐
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
Runloop, auto release pool, thread, GCD
Advanced C language: pointer (2)
Research on cookies in WebView
Introduction to original code, inverse code and complement code
18张图,直观理解神经网络、流形和拓扑
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
安全狗入选《云安全全景图2.0》多个细项
The safety dog has been selected into many details of cloud security panorama 2.0
This year, MediaTek 5g chip shipments are expected to reach 50million sets!
Kotlin JVM annotation
Thesis reading (0) - alexnet of classification
Cnpm installation steps
PCA learning
1.8tft color screen test code (stm32f407ve)
【复制】互联网术语、简称、缩写
Mgr.exe virus caused the startup program to fail
Reading of "robust and communication efficient federated learning from non-i.i.d. data"
Thesis reading (3) - googlenet of classification
Source code analysis of kotlin collaborative process startup
