1,init():类的初始化方法(构造方法)
class Student: NSObject { //类属性 var name:String = "" var age:Int = 0 //类构造函数 init(newName:String,newAge:Int){ self.name = newName self.age = newAge } //成员函数 func run() -> String { return "\(name) can run" } }
var student = Student(newName:"baibai",newAge:31) student.name = "张三" print(student.name) print(student.age) print(student.run())