Swift笔记 - 07.单例模式、方法、下标、返回值、初始化

Swift笔记 - 07.单例模式、方法、下标、返回值、初始化

单例模式

public class FileManager {
    public static let shared = FileManager()
    private init() { }
}

public class FileManager {
    public static let shared = {
        // ...
        // ...
        return FileManager()
    }()
    private init() { }
}

方法(Method)

class Car {
    static var cout = 0
    init() {
        Car.cout += 1
    }
    static func getCount () -> Int { cout }
}
let c0 = Car()
let c1 = Car()
let c2 = Car()
print(Car.getCount ()) // 3

mutating关键字

struct Point {
    var x = 0.0, y = 0.0

    mutating func moveBy(deltaX: Double, deltaY: Double) {
        x += deltaX
        y += deltaY
        // self = Point(x: x + deltaX, y: y + deltaY)
    }
}

enum StateSwitch {
    case low, middle, high

    mutating func next() {
        switch self {
        case .low:
            self = .middle
        case .middle:
            self = .high
        case .high:
            self = .low
        }
    }
}

@discardableResult

struct Point {
    var x = 0.0, y = 0.0
    @discardableResult mutating
    func moveX(deltaX: Double) -> Double {
        x += deltaX
        return x
    }
}
var p = Point()
p.moveX(deltaX: 10)

@discardableResult
func get() -> Int {
    return 10
}
get()

下标 (subscript)

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 == 14 {
                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
class Point {
    var x = 0.0, y = 0.0
    subscript(index: Int) -> Double {
        get {
            if index == 0 {
                return x
            } else if index == 1 {
                return y
            }
            return 0
        }
    }
}
class Point {
    var x = 0.0, y = 0.0
    subscript(index: Int) -> Double {
        if index == 0 {
            return x
        } else if index == 1 {
            return y
        }
        return 0
    }
}
class Point {
    var x = 0.0, y = 0.0
    subscript(index i: Int) -> Double {
        if i == 0 {
            return x
        } else if i == 1 {
            return y
        }
        return 0
    }
}

var p = Point()
p.y = 22.2
print(p[index: 1]) // 22.2

下标可以是类型方法

class Sum {
    static subscript(v1: Int, v2: Int) -> Int {
        return v1 + v2
    }
}
print(Sum[10, 20])

结构体、类作为返回值对比

class Point {
    var x = 0, y = 0
}

class PointManager {
    var point = Point()
    subscript(index: Int) -> Point {
        get {
            point
        }
    }
}

var pm = PointManager()
pm[0].x = 11
pm[0].y = 22
// Point (x: 11, y: 22)
print(pm[0])
// Point (x: 11, y: 22)
print(pm.point)

struct Point {
    var x = 0, y = 0

    class PointManager {
        var point = Point()
        subscript(index: Int) -> Point {
            set {
                point = newValue
            }
            get {
                point
            }
        }
    }
}
class Grid {
    var data = [
        [0, 1, 21],
        [3, 4, 5],
        [6, 7, 8]
    ]
    subscript(row: Int, column: Int) -> Int {
        set {
            guard row >= 0 && row < 3 && column >= 0 && column < 3 else {
                return
            }
            data[row][column] = newValue
        }
        get {
            guard row >= 0 && row < 3 && column >= 0 && column < 3 else {
                return 0
            }
            return data[row][column]
        }
    }
}

var grid = Grid()
grid[0, 1] = 77
grid[1, 2] = 88
grid[2, 0] = 99
print(grid.data)

继承

重写实例方法、下标

class Animal {
    func speak() {
        print ("Animal speak")
    }
    subscript (index: Int) -> Int {
        return index
    }
}

var anim: Animal
anim = Animal()
// Animal speak
anim.speak()
// 6
print(anim[6])
class Cat: Animal {
    override func speak() {
        super.speak()
        print ("Cat speak")
    }
    override subscript (index: Int) -> Int {
        return super [index] + 1
    }
}

anim = Cat()
// Animal speak
// Cat speak
anim. speak() // 7
print(anim[6))
class Animal {
    class func speak() {
        print ("Animal speak")
    }
    class subscript(index: Int) -> Int {
        return index
    }
}
// Animal speak
Animal.speak()
// 6
print(Animal[6])
class Cat : Animal {
    override class func speak() {
        super.speak()
        print ("Cat speak")
    }
    override class subscript (index: Int) -> Int {
        return super[index] + 1
    }
}
// Animal speak
// Cat speak
Cat.speak() // 7
print (Cat [6])

重写属性

class Circle {
    static var radius: Int = 0
    class var diameter: Int {
        set {
            print("Circle setDianeter")
            radius = newValue / 2
        }
        get {
            print("Circle getDianeter")
            return radius * 2
        }
    }
}

class SubCircle: Circle {
    override static var diameter: Int {
        set {
            print("SubCircle setDianeter")
            super.diameter = newValue > 0 ? newValue : 0
        }
        get {
            print("SubCircle getDianeter")
            return super.diameter
        }
    }
}

Circle.radius = 6
// Circle getDianeter
// 12
print(Circle.diameter)
// Circle setDianeter
Circle.diameter = 28
// 10
print(Circle.radius)

SubCircle.radius = 6
// SubCircle getDianeter
// Circle getDianeter
// 12
print(Circle.diameter)
// SubCircle setDianeter
// Circle setDiameter
SubCircle.diameter = 20
// 10
print(SubCircle.radius)

属性观察器

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()
// SubCircle willSetRadius 10
// SubCircle didSetRadius 1 10
circle.radius = 10
class Circle {
    var radius: Int {
        set {
            print("Circle setRadius", newValue)
        }
        get {
            print("Circle getRadius")
            return 20
        }
    }
}

class SubCircle: Circle {
    override var radius: Int {
        willSet {
            print("SubCircle willetRadius", newValue)
        }
        didSet {
            print("SubCircle didSetRadius", oldValue, radius)
        }
    }
}

var circle = SubCircle()
// Circle getRadius
// SubCircle willSetRadius 10
// Circle setRadius 10
// Circle getRadius
// SubCircle didSetRadius 20 20
circle.radius = 10
class Person {
    var age: Int {
        willSet {
            print("willet", newValue)
        }
        didSet {
            print("didSet", oldValue, age)
        }
    }
    init() {
        self.age = 0
    }
}

class Student: Person {
    override init() {
        super.init()
        self.age = 1
    }
}

// willSet 1
// didSet 0 1
var stu = Student()

final

初始化器

//指定初始化器
init(parameters) {
    statements
}

//便捷初始化器
convenience init(parameters) {
    statements
}

初始化器的相互调用

两段式初始化

安全检查

自动继承

  1. 如果子类没有自定义任何指定初始化器,它会自动继承父类所有的指定初始化器
  2. 如果子类提供了父类所有指定初始化器的实现(要么通过方式1继承,要么重写)
    • 子类自动继承所有的父类便捷初始化器
  3. 就算子类添加了更多的便捷初始化器,这些规则仍然适用
  4. 子类以便捷初始化器的形式重写父类的指定初始化器,也可以作为满足规则2的一部分

required 关键字

class Person {
    required init() { }
    init(age: Int) { }
}
class Student: Person {
    required init() {
        super.init()
    }
}

可失败初始化器

class Person {
    var name: String

    init?(name: String) {
        if name.isEmpty {
            return nil
        }
        self.name = name
    }
}
var num = Int("123")
public init?(_ description: String)

enum Answer : Int {
    case wrong, right
}
var an = Answer(rawValue: 1)

反初始化器

class Person {
    deinit {
        print("Person对象销毁了")
    }
}

版权所有,转载请注明出处 luowei.github.io.