美文网首页
Swift 2 学习笔记 12.类

Swift 2 学习笔记 12.类

作者: Maserati丶 | 来源:发表于2018-11-21 20:32 被阅读0次

    课程来自慕课网liuyubobobo老师


    • 类的基础
    class Person {
        var firstName: String
        var lastName: String
        
        init(firstName: String, lastName: String) {
            self.firstName = firstName
            self.lastName = lastName
        }
        
        init?(fullName: String) {
            if let spaceIndex = fullName.index(of: " ") {
                self.firstName = String(fullName[..<spaceIndex])
                self.lastName = String(fullName[fullName.index(after: spaceIndex)..<fullName.endIndex])
            }else {
                return nil
            }
        }
        
        func fullName() -> String {
            return self.firstName + " " + self.lastName
        }
    }
    
    let person1 = Person(firstName: "Alexander", lastName: "Hamilton")
    person1.fullName()  // Alexander Hamilton
    let person2 = Person(fullName: "Alexander Hamilton")
    
    • 类是引用类型
    class Person {
        var firstName: String
        var lastName: String
        var career: String?
        
        init(firstName: String, lastName: String, career: String) {
            self.firstName = firstName
            self.lastName = lastName
            self.career = career
        }
        
        init(firstName: String, lastName: String) {
            self.firstName = firstName
            self.lastName = lastName
        }
        
        func fullName() -> String {
            return self.firstName + " " + self.lastName
        }
    }
    
    let person1 = Person(firstName: "Yubo", lastName: "Liu", career: "Developer")
    
    let person2 = person1
    person2.career = "CEO"
    person1.career  // "CEO"
    
    值类型
    引用类型
    • 引用类型的特点
    class Person {
        var firstName: String
        var lastName: String
        var career: String?
        
        init(firstName: String, lastName: String, career: String) {
            self.firstName = firstName
            self.lastName = lastName
            self.career = career
        }
        
        init(firstName: String, lastName: String) {
            self.firstName = firstName
            self.lastName = lastName
        }
        
        func fullName() -> String {
            return self.firstName + " " + self.lastName
        }
        
        func changeCareer(newCareer: String) {
            self.career = newCareer
        }
    }
    
    let person = Person(firstName: "Yubo", lastName: "Liu", career: "Developer")
    person.changeCareer(newCareer: "CEO")
    
    • 类的等价
    let person1 = Person(firstName: "Yubo", lastName: "Liu", career: "Developer")
    let person2 = person1
    
    person1 === person2  // true
    
    let person3 = Person(firstName: "Yubo", lastName: "Liu", career: "Developer")
    person1 === person3  // false
    person1 !== person3  //  true
    
    • 什么时候使用结构体?什么时候使用类?
      1.把结构体看作是值,把类看作是物体
      2.结构体是值类型,类是引用类型
      3.类可以被继承
      4.结构体比类更轻量级

    相关文章

      网友评论

          本文标题:Swift 2 学习笔记 12.类

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