swift中对象和结构体的使用
时间:2019-10-08 16:56:56
收藏:0
阅读:108
- 对象
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
原文:https://www.cnblogs.com/hualuoshuijia/p/11636187.html
评论(0)