import UIKit var str = "Hello,playground" // 协议和扩展 protocol ExampleProtocol { var simpleDescription:String {get} mutating func adjust() } class simpleClass: ExampleProtocol { var simpleDescription: String = "A very simple class" var anotherProperty:Int = 69105 func adjust() { simpleDescription += "Now 100% adjusted" } } var a = simpleClass() a.adjust() let aDescription = a.simpleDescription struct SimpleStructure:ExampleProtocol { var simpleDescription: String = "A simple structure" mutating func adjust() { simpleDescription += " (adjusted)" } } var b = SimpleStructure() b.adjust() let bDescription = b.simpleDescription extension Int : ExampleProtocol{ var simpleDescription: String{ return "The number \(self)" } mutating func adjust() { self += 42 } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。