• swift中对象和结构体的使用

    • 对象 
    class Student{
       
        var name: String = "";
        var age: Int = 0;
        
        init(name: String ,age:Int) {
            self.name = name
            self.age = age
        }
    }
    
    let stu = Student(name: "xiao", age: 18)
    print("姓名: \(stu.name)")//姓名: xia

     

    • 结构体
    struct Person{
        
        var name: String = "";
        var age: Int = 0;
        
        init(name: String ,age:Int) {
            self.name = name
            self.age = age
        }
    }
    
    let person = Person(name: "xiao", age: 18)
    print("姓名: \(person.name)")//姓名: xiao
    

     

    结构体不需要考虑引用计数,接头体是值赋值,就是a赋值给b,如果改变b,则a不变,举例

    let person = Person(name: "xiao", age: 18)
    print("姓名: \(person.name)")//姓名: xiao
    var per1 = person;
    per1.name = "da"
    
    print("姓名: \(person.name)")//姓名: xiao
    
    相关文章
    相关标签/搜索
    一,肖一码′期期准