美文网首页
19.swift-类的存储属性&计算属性&属性监听

19.swift-类的存储属性&计算属性&属性监听

作者: ChaosHeart | 来源:发表于2021-07-22 02:15 被阅读0次

//: Playground - noun: a place where people can play

import UIKit

class Person:NSObject{
//属性监听器
var name:String?{
//属性即将改变时进行监听
willSet{
print(newValue as Any);
}
//属性已经改变时进行监听
didSet{
print(oldValue as Any);
}
}
}

var p = Person();
p.name = "why";
p.name = "zy";

class Student:NSObject {
//存储属性
var age:Int = 20;

//计算属性
var name:String? {
    //get计算属性
    "小明"; //默认名字为小明
}
//计算属性
var num: Int {
    set {
        self.age = newValue;
    }
    get {
        return 22;
    }
}

//属性贯穿
var six:String = "男" {
    willSet {
        print("新: \(newValue)");
    }
    didSet {
        print("旧: \(oldValue)");
    }
}

}

var s = Student();
print("名字: (s.name!)");
s.num = 22;
print(s.age)
s.six = "女";

相关文章

网友评论

      本文标题:19.swift-类的存储属性&计算属性&属性监听

      本文链接:https://www.haomeiwen.com/subject/iyrzlltx.html