//默认构造器 class Student{ var name : String = "" var number : Int = 0 } //自定义构造函数 class Person{ //雷属性 var name:String var age:Int //类构造函数 init(newName:String,newAge:Int) { self.name = newName self.age = newAge } //实例函数 func say() -> String { return "我叫MT(name)" } }
class People { //普通属性 var firstName:String = "" var lastName:String = "" var nickName:String = "" //计算属性 var fullName:String { get { return nickName + " " + firstName + " " + lastName } } //带属性监视器普通属性 var age:Int = 0 { //属性变化前 willSet{ print("will set an new value(age) to age") } didSet{ print("age file change form (oldValue) to (age)") if age < 10 { nickName = "Little" }else{ nickName = "Big" } } } func toString() ->String{ return "Full name (fullName) +_ age(age)" } }