Swift 类

作者: 诗颜语 | 来源:发表于2022-04-19 11:43 被阅读0次
//
//  Class.swift
//  swift_clt
//
//  Created by wuhongjia on 2021/8/16.
//
 
 
/**
  
    - 类对象的  计算属性与方法 通过 `static/class`关键字修饰,存储属性通过 `static`关键字修饰
    - `static`修饰后 子类**不可**继承重写
    - `class`修饰后 子类**可以**继承重写
     
 */
class Animal {
    //MARK: - 类对象 存储属性
    static var labelName = "动物" {
        willSet {
            print("willSet -", newValue)
        }
        didSet {
            print("didSet - ",oldValue)
        }
    }
     
    //MARK: - 类对象 计算属性
    class var type: String {
        get {
            labelName
        }
        set {
            labelName = newValue
        }
    }
     
    //MARK: - 类对象 方法
    static func sleep () {
        print(labelName + "is sleep")
    }
     
    class func weakUp () {
        print(labelName + "is weak up")
    }
 
 
}
 
 
/**
 - 存储属性:会分配实际的存储空间的属性。可以通过`willSet/didSet` 去监听值的改变
 - 计算属性:长得像属性,实际是方法。使用`get/set`表示,可以只有`get`
 - 懒加载属性:属于存储属性,不必再`init()`时初始化,使用时才会。使用`lazy var`修饰
 - 只读属性:有可能是存储属性,也有可能是计算属性,看例子
 - 父类:父类是放在 冒号`:`右侧
 - 协议:类遵守协议是放在 冒号`:`右侧,如果有父类或者多个,在父类右侧使用`,`隔开
 - 方法:使用`func`修饰
 */
 
 
class Cat: Animal, CustomStringConvertible, CustomDebugStringConvertible {
    //MARK: - 类对象 继承重写方法
    override class func weakUp() {
        print("Cat is weak up")
    }
     
    //MARK: - 实例对象 存储属性
    let butt: String = {
        let butt = "屁股"
        return butt
    }()
    var head = "头"
    /// 站着呢?
    var standing = true {
        willSet {
            print("willSet -", newValue)
        }
        didSet {
            print("didSet - ",oldValue)
        }
    }
    /// 懒加载
    lazy var tail = "尾巴"
     
    lazy var beard: String = {
        let beard = "胡须"
        return beard
    }()
   
     
    //MARK: - 实例对象 计算属性
    var foot: String {
        get {
            standing ? "两只脚丫": "四只脚丫"
        }
        set {
            standing = newValue == "两只脚丫"
        }
    }
     
    //MARK: - 只读
    /// 方式一  计算属性 与  存储属性结合使用
    var isStanding: Bool {
        get { // get 可省略
            standing
        }
    }
    /// 方式二  存储属性 通过访问控制关键字修饰
    private (set) var isBeautiful = true
    fileprivate (set) var isLovely = true
 
 
     
    //MARK: - <CustomStringConvertible>
     
    var description: String {
         
        return  """
                 
                ------Print----------
                \(head)
                \(standing)
                \(foot)
                ------PrintEnd------
                """
    }
     
     
    //MARK: - <CustomDebugStringConvertible>
     
    var debugDescription: String {
         
        return  """
                 
                ------Debug----------
                \(head)
                \(standing)
                \(foot)
                ------DebugEnd-------
                """
    }
     
     
    //MARK: - 实例对象 方法
    func run() {
         
        print("cat is run")
        print(foot)
        standing = false
        print(foot)
        print("cat run end")
         
        print(standing)
        foot = "两只脚丫"
        print(standing)
        print(butt)
    }
}
 
 
/**
 - 拓展:使用 `extension`修饰,可以拓展 计算属性/方法
 */
extension Cat {
     
    var age: Int {
        get {
            10
        }
        set {
            print(newValue)
        }
    }
     
     
    func run1(name: String, age: Int) -> Int {
        return 10
    }
 
 
}
 
 
func class_run() {
    let cat = Cat()
    cat.run()
     
    // CustomStringConvertible, CustomDebugStringConvertible 关注这两个协议可以实现 description/debugDescription
    print("\n\n--CustomStringConvertible, CustomDebugStringConvertible--")
    print(cat)
    debugPrint(cat)
     
    print("\n\n--懒加载--")
    print(cat.tail)
    print(cat.beard)
 
 
 
 
    print("\n\n--只读--")
//    cat.isStanding = false // 不要改啦,小猫猫只想站着  无法set
//    cat.isBeautiful = false // 小猫猫难道不漂亮?!    无法set
    cat.isLovely = false // 小猫猫难道不可爱?!         可以set  原因是fileprivate 关键字 是说当前文件内set是私有的 当前文件内set可访问
    print(cat.isStanding) // 小猫站着
    print(cat.isBeautiful) // 小猫美丽
    print(cat.isLovely) // 小猫可爱
 
 
     
    // 类的 属性/方法
    print("\n\n--类的 属性/方法--")
    print(Cat.labelName)
    print(Cat.type)
    Cat.sleep()
    Cat.weakUp() // 被子类重写了
     
}

相关文章

网友评论

      本文标题:Swift 类

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