标签:
protocol Example{
var simpleDescription:String{get}
mutating func adjust()
}
// 类继承协议
class Simple:Example{
var simpleDescription = "Sing"
func adjust(){
println("description: \(simpleDescription)")
}
}
var simple = Simple()
simple.simpleDescription
simple.adjust()
// 结构体继承协议
struct SimpleStructure:Example{
var simpleDescription = "struct"
mutating func adjust(){
println("This is \(simpleDescription)")
}
}
var str = SimpleStructure()
str.simpleDescription
str.adjust()
// 枚举继承协议
enum simpleEnum: Example{
case first(String)
var simpleDescription:String{
get{
switch self{
case let .first(text): return text
default: return "hi"
}
}
}
mutating func adjust(){
print("description: hi")
}
}
var s = simpleEnum.first("dingding")
s.simpleDescription
s.adjust()
标签:
原文地址:http://www.cnblogs.com/lianfu/p/5052643.html