protocol Example{繼承
var simpleDescription:String{get}ip
mutating func adjust()get
}it
// 類繼承協議io
class Simple:Example{class
var simpleDescription = "Sing"im
func adjust(){協議
println("description: \(simpleDescription)")di
}co
}
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()