标签:des style class blog code color
一个类可以继承(inherit)另一个类的方法(methods),属性(property)和其它特性一、基本语法
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) } }
标签:des style class blog code color
原文地址:http://blog.csdn.net/xbenlang/article/details/32129641