声明的关键字就是protocol
我们看一个官方例子
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.simpleDescriptionprint(aDescription)
类中的方法因为本身就可以改变类所以adjust方法不需要mutating关键字修饰
结构体使用协议
struct SimpleStructure: ExampleProtocol { var simpleDescription: String = "A simple structure" mutating func adjust() { simpleDescription += " (adjusted)" }}var b = SimpleStructure()b.adjust()let bDescription = b.simpleDescriptionprint(bDescription)
结构体重的方法如果需要改变结构体本身 使用的方法需要使用mutating进行修饰,如果说我们在上面的adjust中不对结构体做任何修改的话,比如说单纯的打印一段文字,不加mutating也是可以的。
enum SimpleEnum:ExampleProtocol { case out mutating func adjust() { print(self.simpleDescription) } var simpleDescription: String{ get { switch self { case .out: return "A simple enumeration" } } }}var c = SimpleEnum.outc.adjust()因为官方没看到enum的列子,就找个模照着谢了一个
生命扩展的关键字是extension
我们可以使用扩展为已有的类型添加属性或者方法,我们也可以使用扩展将协议使用在已有的类型中
protocol ProtocolForInt { var description:String {get} mutating func addOne()}extension Int:ProtocolForInt { mutating func addOne() { self += 1 } var description: String { return "This number is \(self)" }}var a:Int = 4a.addOne()print(a.description)
我们写一个类,同样也是使用上面的协议
<pre name="code" >class SimpleClassForInt:ProtocolForInt { var number = 0 var description: String = "这是一个描述" func addOne() { number += 1 }}var simpleClassForInt:ProtocolForInt = SimpleClassForInt()print(simpleClassForInt.description)simpleClassForInt.addOne()//print(simpleClassForInt.number)我们可以看到只有协议中定义的属性和方法使我们可以访问的
类中在协议中没有定义的属性,我们是无法访问的。 尽管simpleClassForInt的运行时(Runtime)类型是SimpleClassForInt,但是编译器还是会把它视为ProtocolForInt类型,所以在协议之外的属性和方法在simpleClassForInt中是无法访问的。
总结以上是内存溢出为你收集整理的swift3新路程(10)协议protocol和扩展extension全部内容,希望文章能够帮你解决swift3新路程(10)协议protocol和扩展extension所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)