存儲(chǔ)屬性
定義:實(shí)實(shí)在在存儲(chǔ)常量和變量的
計(jì)算屬性
定義:依賴于存儲(chǔ)屬性奴潘,計(jì)算得出
類屬性
定義:其本質(zhì)其實(shí)就是一個(gè)全局屬性旧烧,在類里面限定了其作用域罷了
懶加載屬性
定義:該屬性在使用的時(shí)候初始化一次
全局屬性
定義:類外面的屬性,作用域全局画髓。掘剪。正因?yàn)槿绱耍琒wift應(yīng)該不能成為真正的oop和ood吧
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let p = People()
print(p.name)
print(p.age)
print(p.name)
print(p.name)
print(p.name)
print(People.height)
print(People.height)
print(People.height)
print(p.getAge)
print(p.getAge)
print(p.getAge)
print(commonProperty)
print(commonProperty)
print(commonProperty)
}
}
//全局屬性
var commonProperty = {()->String in
print("Common Property1")
return "Common Property"
}()
class People{
//類屬性
static var height = {()->Int in
print("Static Properties")
return 170
}()
//存儲(chǔ)屬性
var age = {()->Int in
print("Store properties")
return 26
}()
//計(jì)算屬性
var getAge:Int{
get{
print("Computed Properties")
return age
}
}
//懶加載屬性
lazy var name = {()->String in
print("Lazy Properties")
return "Lazy liyang"
}()
//構(gòu)造方法
init(){
print("init")
}
}
輸出結(jié)果:
Store properties
init
Lazy Properties
Lazy liyang
26
Lazy liyang
Lazy liyang
Lazy liyang
Static Properties
170
170
170
Computed Properties
26
Computed Properties
26
Computed Properties
26
Common Property1
Common Property
Common Property
Common Property
總結(jié):基于類奈虾,類的構(gòu)造
1.存儲(chǔ)屬性,最先初始化
2.構(gòu)造方法,僅次于存儲(chǔ)屬性調(diào)用夺谁,可以在這里對(duì)存儲(chǔ)屬性進(jìn)行賦值
3.懶加載屬性、類屬性肉微、全局屬性都是在第一次使用的時(shí)候初始化一次匾鸥,以后調(diào)用都不在初始化
warning:當(dāng)懶加載屬性是基于一個(gè)存儲(chǔ)屬性計(jì)算的時(shí)候,切勿使用懶加載屬性碉纳,采用計(jì)算屬性