swift中对象和结构体的使用

こ雲淡風輕ζ 提交于 2019-11-30 18:59:30
  • 对象 
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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!