swift3新路程(10)协议protocol和扩展extension
发布时间:2020-12-14 06:43:16 所属栏目:百科 来源:网络整理
导读:声明的关键字就是protocol 我们看一个官方例子 protocol ExampleProtocol { var simpleDescription: String { get } mutating func adjust()} 枚举、结构体和类都可以使用协议 类使用协议 class SimpleClass: ExampleProtocol { var simpleDescription: Stri
声明的关键字就是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.simpleDescription print(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.simpleDescription print(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.out c.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 = 4 a.addOne() print(a.description)
我们写一个类,同样也是使用上面的协议 <pre name="code" class="plain">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中是无法访问的。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Cocos2d-X使用CCAnimation创建动画
- 为什么Ruby定义Object#=??
- c# – 获取DayOfWeek值的本地化字符串
- ruby中的back-tick与tail -f命令不兼容
- xml2.encode2,xml
- cocos2d-x使用之调用系统消息框CCMessageBox
- xml – 如何定义几个相同名称的元素,但xsd:choice元素中的
- c# – XML序列化 – XmlCDataSection为Serialization.XmlTe
- ruby-on-rails – 如何使用Twitter Bootstrap Rails gem定义
- c# – 检查重复项并从DataTable中删除数据库表的好方法?