方法、下标、继承
方法
枚举、结构体、类都可以定义实例方法、类型方法
- 实例方法: 通过实例调用
- 类型方法: 通过类型调用,用static或者class关键字定义
class Car {
static var cout = 0
init() {
Car.cout += 1
}
static func getCout() -> Int { cout }
}
let c0 = Car()
let c1 = Car()
let c2 = Car()
print(Car.getCout()) // 3
/*
self 在实例方法中代表实例 在类型方法中代表类型
在类型方法 static func getCout() 中 cout 等价于 self.cout、Car.self.cout、Car.cout
*/
关键字 mutating @discardableResult
-
mutating
- 结构体和枚举是值类型,默认情况下,值类型的属性不能被自身的实例方法修改
- 在func关键字前加mutating可以允许这种修改行为
-
@discardableResult
- 在func前面加个@discardableResult可以消除: 函数调用后返回值未使用的警告
struct Point {
var x = 0.0 , y = 0.0
@discardableResult mutating func move(x1: Double, y1: Double) -> Double{
x += x1
y += y1
return x + y
}
}
var p = Point()
p.move(x1: 2, y1: 2)
下标 subscript
- 使用subscript可以给任意类型(枚举、结构体、类)添加下标功能, 有些地方翻译为 下标脚本
- subscript的语法类似实例方法、计算属性,本质就是方法(函数)
- subscript 可以没有set方法,但必须要有get方法,如果只有get方法可以省略get
- 可以设置 参数标签
- 下标可以是类型方法
- 下标可以传入多个参数
class Point {
var x = 0.0, y = 0.0
subscript(index: Int) -> Double {
set {
if index == 0 {
x = newValue
} else if index == 1{
y = newValue
}
}
get {
if index == 0 {
return x
} else if index == 1{
return y
}
return 0
}
}
}
var p = Point()
p[0] = 11.1
p[1] = 22.2
print(p.x) //11.1
print(p.y) //22.2
print(p[0]) //11.1
print(p[1]) //22.2
// subscript 中定义的返回值类型决定了
// get 方法的返回值类型
// set 方法中newValue的类型
// subscript 可以接收多个参数,并且类型任意
结构体、类作为返回值对比
struct Point {
var x = 10 , y = 10
}
class PointManager {
var point = Point()
subscript(index: Int) -> Point {
// 当point是结构体值类型,修改里面的值必须加set方法
set { point = newValue }
get { point }
}
}
var pm = PointManager()
pm[0].x = 11 // 本质为 pm[0] = Point(x: 11, y: pm[0].y)
pm[0].y = 22 // 本质pm[0] = Point(x: pm[0].x, y: 22)
class Point {
var x = 10 , y = 10
}
class PointManager {
var point = Point()
subscript(index: Int) -> Point {
// 当point是结类,修改里面的值不需要set方法
get { point }
}
}
var pm = PointManager()
pm[0].x = 11
pm[0].y = 22
// pm[0]实际是个指针变量,可以访问堆空间的变量
继承
-
只有类支持继承,值类型(枚举、结构体)不支持
-
没有父类的类,称为 基类
- Swift没有像OC、Java那样的规定:任何类最终都要继承自某个基类
-
子类可以重写父类的下标、方法、属性,重写必须加上 override关键字
重写实例方法、下标
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 = Cat()
anim.speak() //Animal speak Cat speak
print(anim[7]) // 8
重写类型方法、下标
- 被class 修饰的类型方法、下标允许被子类重写
- 被static 修饰的类型方法、下标不允许被子类重写
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
}
}
Cat.speak() //Animal speak Cat speak
print(Cat[7]) // 8
重写属性
- 子类可以将父类的属性(存储、计算)重写为计算属性
- 子类不可以将父类属性重写为存储属性
- 只能重写var属性不能重写let属性
- 重写时,属性名、类型要一致
- 子类重写后的属性权限,不能小于父类属性的权限
- 如果父类属性时只读的,那么子类重写后的属性可以是只读的,也可以是可读写的
- 如果父类属性时读写的,那么子类重写后的属性必须是读写的
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()
print(circle.radius)
//class Circle {
// class var radius: Int {
// set {
// print("Circle setRadius", newValue)
// }
// get {
// print("Circle getRadius")
// return 20
// }
// }
//}
// SubCircle setRadius
circle.radius = 6
// SubCircle getDiameter
// Circle getDiameter
// SubCircle getRadius
// 12
print(circle.diameter)
// SubCircle setDiameter
// Circle setDiameter
// SubCircle setRadius
circle.diameter = 20
// SubCircle getRadius
// 10
print(circle.radius)
- 重写类型属性
- 被class修饰的计算类型属性可以被子类重写
- 被static修饰的类型属性(存储、计算)不可以被子类重写
属性观察器
可以在子类中为父类属性(除了只读计算属性、let属性)增加属性观察器
class Circle {
var radius: Int = 1
}
class SubCircle : Circle {
// 并没有将radius 重写为计算属性
override var radius: Int {
willSet {
print("SubCircle willSetRadius",newValue)
}
didSet {
print("SubCircle didSetRadius",oldValue,radius)
}
}
}
var circle = SubCircle()
//SubCircle willSetRadius 10
//SubCircle didSetRadius 1 10
circle.radius = 10
多态的实现原理
/*
多态的实现原理:
1.OC: Runtime
2.C++: 虚表 虚函数表
3.Swift: 类似于虚表,程序一编译就将函数地址放在了类型信息里面了(全局区),通过固定的偏移量去查询函数地址,从而调用
*/
class Animal {
func speak() {
print("Animal speak")
}
func eat() {
print("Animal eat")
}
func sleep() {
print("Animal sleep")
}
}
class Dog: Animal {
override func speak() {
print("Dog speak")
}
override func eat() {
print("Dog speak")
}
func run() {
print("Dog run")
}
}
var anim = Animal()
anim.speak() // 0x1000019a9 <+137>: callq *0x50(%rcx)
anim.eat() // 0x100001a09 <+233>: callq *0x58(%rcx)
anim.sleep() // 0x100001a09 <+233>: callq *0x58(%rcx)
anim = Dog()
anim.speak()
anim.eat()
anim.sleep()
/*
movq 0x1d6a(%rip), %rax // 0x1d6a(%rip)是全局变量的地址值 movq 是将地址指向的堆空间取出8个字节给到rax
movq (%rax), %rcx // rax 是堆空间地址值 是 Dog的前8个字节 指向的一块的内存就是 类型信息
callq *0x50(%rcx) // 类型信息的地址 + 偏移量就能找到 一个函数列表 从而执行
*/