swift语言点评二十一-协议
2024-08-31 12:25:05
定义有什么,及哪些必须实现。
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.
Property Requirements
The protocol doesn’t specify whether the property should be a stored property or a computed property—it only specifies the required property name and type.
protocol SomeProtocol {
var mustBeSettable: Int { get set }
var doesNotNeedToBeSettable: Int { get }
}
If you mark a protocol instance method requirement as mutating
, you don’t need to write the mutating
keyword when writing an implementation of that method for a class. The mutating
keyword is only used by structures and enumerations.
Class-Only Protocols
You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObject
protocol to a protocol’s inheritance list.
protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
// class-only protocol definition goes here
协议联合体作为参量
func wishHappyBirthday(to celebrator: Named & Aged) {
print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
}
let birthdayPerson = Person(name: "Malcolm", age: 21)
wishHappyBirthday(to: birthdayPerson)
最新文章
- Windows Server 2012 虚拟化实战:存储(二)
- phpcms调用一级栏目和二级栏目
- c# 结构体、枚举类型及函数调用
- 如何将后台传来的json反序列化为前端具体对象
- JSON 教程学习进度备忘
- HDU4453--Looploop (Splay伸展树)
- Find Peak Element 解答
- [问题解决] ubuntu server12.04 认证的问题
- experss框架—基础认识
- 【JAVAWEB学习笔记】03_JavaScript
- 聊聊keep-alive组件的使用及其实现原理
- Sony索尼数码录音笔MSV格式转换为MP3格式【转】
- 005_elasticsearch的数据导入导出
- SO_REUSEADDR SO_REUSEPORT
- Linux 防火墙firewalld
- tr,td高度不生效
- 汉诺塔X
- 关于git的基本使用
- jquery插件中(function ( $, window, document, undefined )的作用
- Java 正则表达式 Pattern &; Matcher
热门文章
- Linux top命令简解
- windows下git server搭建
- ZBrush设计制作小怪兽并用KeyShot渲染
- idea--IntelliJ IDEA隐藏不想看到的文件或文件夹
- luoguP5055 【模板】可持久化文艺平衡树 可持久化非旋转treap
- Build rpm example:zram
- Chrome Is The New C Runtime
- Layui表格编辑【不依赖Layui的动态table加载】
- React:关于虚拟DOM(Virtual DOM)
- List<;T>;与List<;?>;的区别