Swift 基本语法(八)— 为类、结构体添加属性、方法

2019-10-19  本文已影响0人  Eddiegooo

为结构体、类、枚举添加属性,设置属性观察器。 添加方法

//为类、结构体、枚举添加属性
struct Point {
    var x = 0 //这里还是遵循常量用let 变量var
    var y = 0
}
struct Size {
    var width = 0
    var height = 0
}

struct Rect {
    //存储属性
    var origin: Point
    var size: Size
    //计算属性
    var Center: Point {
        get {
            return Point(x: origin.x + size.width / 2, y: origin.y + size.height / 2)
        }
        set(newCenter) {
            origin.x = newCenter.x - size.width / 2
            origin.y = newCenter.y - size.height / 2
        }
    }
    //省略写法
//    var Center: Point {
//        get {
//            Point(x: origin.x + size.width / 2, y: origin.y + size.height / 2)
//        }
//        set { //这里自动有newValue
//            origin.x = newValue.x - size.width / 2
//            origin.y = newValue.y - size.height / 2
//        }
//    }
}

//属性观察器

class StepCounter {
    //为局部属性设置属性观察器
//    var totalSteps: Int = 0 {
//        willSet(newStep) {
//            print("will to change newStep :\(newStep)")
//        }
//        didSet(oldSteps) {
//            print("did change form oldStep = \(oldSteps) to \(totalSteps)")
//        }
//    }
    //系统默认值写法
    var totalSteps�: Int = 0 {
        willSet {
            print("will change to newStep: \(newValue)")
        }
        didSet {
            print("did change from oldStep \(oldValue) to \(totalSteps)")
        }
    }
    
    var originalStep = 0
    init() {
        print("step counter init")
    }
}

let counter = StepCounter()
counter.totalSteps = 10
counter.totalSteps = 320

//为全局变量设置属性观察器
var count: Int = 0 {
    willSet {
        print("will change to :\(newValue)")
    }
    didSet {
        print("did change from :\(oldValue) to \(count)")
    }
}
count = 10

懒加载及属性修饰符:

class CustomClass {
    lazy var counter: StepCounter //懒加载属性 要价lazy
    var name = "Eddiegooo"
    //只写get方法 属于只读属性。 可以用. 语法访问,一旦初始化 不可以修改
    var origin: Point {
        get {
            return Point(x: origin.x + 5, y: origin.y + 10)
        }
    }
    //类型属性 static
    static var age = 20
    //加上class 关键字,允许子类重写父类的实现
    class var size: Size {
        return Size(width: 10, height: 10)
    }
}
let custom = CustomClass()
custom.name = "Chole"
custom.counter.originalStep = 100 //懒加载属性方式

添加实例方法。 都默认含有self

struct Size {
    var width = 0
    var height = 0
    var count = 0
    func newCount()  {
        count + 2 //这里可以省略self
    }
    func addCount(_ count: Int) -> Int {
        return self.count + count //当名称一致时,带上self 区分属性和形式参数
    }
}

//在实例方法中修改属性,要加关键字mutating
struct Point {
    var x = 0 //这里还是遵循常量用let 变量var
    var y = 0
    mutating func moveBy(_ x: Int, _ y: Int) {
        self.x = x
        self.y = y
    }
    //改变整个self
    mutating func newSelfFunction(x: Int, y: Int) {
        self = Point(x: self.x + x, y: self.y + y * 2)
    }
}
var p = Point(x: 0, y: 0)
p.moveBy(3, 5)
p.newSelfFunction(x: 5, y: 60)
print(p)
//枚举的异变有点意思
enum ThirdStatus {
    case high, low, off
    mutating func next() {
        switch self {
        case .high:
            self = .low
        case .low:
            self = .off
        case .off:
            self = .high
        }
    }
}
var status = ThirdStatus.low
status.next()
status.next()
class SuperClass {
    //使用 class 关键字,允许子类重写父类方法
    class func allowSubClassRewrite() {
        
    }
    //实例方法
    func customFunc() -> Void {
        print("可以使用实例对象的点语法调用")
    }
    //类型方法 static
    static func testStaticFunc() {
        print("不能使用点语法调用哦,使用类直接调,类似OC的类方法")
    }
}

let myClass = SuperClass()
myClass.customFunc()
//myClass.testSta...  找不到方法
SuperClass.testStaticFunc()  //这样子使用
上一篇下一篇

猜你喜欢

热点阅读