当前位置 : 主页 > 手机开发 > 其它 >

swift中对象和结构体的使用

来源:互联网 收集:自由互联 发布时间:2021-06-11
对象 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 结构体 struc
  • 对象 
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
网友评论