一、基本语法
class Human{ var name :String init(){ name = "human" println(name) } func description(){ println("name:\(name)") } } class Student:Human{ var score = 0 init(){ super.init() name = "Student" println(name) } override func description(){ super.description() println("score:\(score)") } }
class Human{ var name :String init(){ name = "human" } func description(){ println(name) } } class Student:Human{ override var name:String{ willSet{ println("will set \(newValue)") } } override func description() { println("Student name:\(name)") } } var a = Student() a.name = "小笨狼" //输出:will set 小笨狼 a.description() //输出:Student name:小笨狼
@final class Human{ //① 在class前加@final,此类将不可被继承 @final var name :String //② 在属性前加@final,属性将不可被重写 init(){ name = "human" } @final func description(){ //③ 在方法前加@final,方法将不可被重写 println(name) } }
原文:http://blog.csdn.net/xbenlang/article/details/32129641