var sex: String?
var identify: PersonIdentify? // 构造器中,第一个參数依旧会创建一个和局部參数名称同样的外部參数名称 init(name: String, sex: String, identify: PersonIdentify){ self.name = name; self.sex = sex; self.identify = identify } func description() -> String{ // 调用枚举值的toRaw()方法能够获取到枚举值的初值 return "name = \(name) sex = \(sex) course = \(identify!.toRaw())" } // 在类中假设实现协议中定义的mutating方法,那么不须要加"mutating"keyword func aMutatingMethod(){ } class func aTypeMethod(){ } } var person1 = Person(name:"humingtao",sex:"m",identify:.Student) var person2 = Person(name:"kebi",sex:"m",identify:.Teacher) switch(person1.identify!){ case .Teacher: println("I am a teacher") case .Student: println("student") } class Student:Person{ var number: String? // 在类中,类型属性仅仅能够是计算属性,可是在枚举和结构体中,既能够是计算属性,也能够是存值属性 // 在类中,声明一个类型属性的keyword是class,在结构体和枚举中是static class var attendClassTime: String{ return "9:30" } init(name: String, sex: String, number: String){ super.init(name: name, sex: sex, identify: .Student) self.number = number } } var student1 = Student(name: "hmt",sex: "m",number: "12345678") class Teacher:Person{ var course: String? init(name: String, sex: String, course: String){ super.init(name: name, sex: sex, identify: .Teacher) self.course = course } override func description() -> String{ return super.description() + "" + "course = \(course)" } } // 思考为什么加"?" class course{ var name: String? init(name: String){ self.name = name } }
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5209449.html,如需转载请自行联系原作者