swift init 初始化

class PropertiesClass1 {
    var a = "Stringa"
    init() {
        print("初始化")
    }


}

class PropertiesClass2 {
    var id  = 0
    //lazy 懶加載,只有使用的時候才觸發
    lazy var properties1 = PropertiesClass1()

    //初始化必須所有初始化全部屬性,或者爲Options
    var name: String? 

    //geter seter
    var qq: String {
        //geter seter 裏面不能使用本身屬性,不然會死循環
        get {
            return self.name!
        }
        set {
            self.name = newValue
        }
    }
    //只讀屬性
    var height: Int {
        return 180
    }

    var address: String = "xxx" {
        willSet {
            print(" 新值\(newValue)")
        }

        didSet {
            print("舊值 \(oldValue)")
        }
    }

    var function_properties = {return "properties"}()

    
}

var properties2 = PropertiesClass2()

print(properties2.properties1)


properties2.qq = "65683264"
print(properties2.qq)

print(properties2.height)
// properties2.height = 2

properties2.address = "ca"

print(properties2.function_properties)
相關文章
相關標籤/搜索