swift语言点评二十一-协议
定义有什么,及哪些必须实现。
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)
swift语言点评二十一-协议的更多相关文章
- swift语言点评二
一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...
- swift语言点评二十-扩展
结论:扩展无法修改原来的数据结构. Extensions can add new functionality to a type, but they cannot override existing ...
- Swift语言指南(二)--语言基础之注释和分号
原文:Swift语言指南(二)--语言基础之注释和分号 注释 通过注释向自己的代码中注入不可执行的文本,作为你自己的笔记或提示.Swift编译器运行时会忽略注释. Swift的注释与C语言极其相似,单 ...
- Swift 学习之二十一:?和 !(详解)
http://blog.csdn.net/woaifen3344/article/details/30244201 Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始 ...
- swift语言点评十二-Subscripts
Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the m ...
- swift语言点评十一-Methods
Assigning to self Within a Mutating Method Mutating methods can assign an entirely new instance to t ...
- 苹果新的编程语言 Swift 语言进阶(十一)--实例的初始化与类的析构
一 .实例的初始化 实例的初始化是准备一个类.结构或枚举的实例以便使用的过程.初始化包括设置一个实例的每一个存储属性为一个初始值,以及执行任何其它新的实例能够使用之前需要的设置或初始 ...
- Swift5 语言指南(二十一) 嵌套类型
通常创建枚举以支持特定类或结构的功能.类似地,定义纯粹在更复杂类型的上下文中使用的实用程序类和结构可能是方便的.为此,Swift允许您定义嵌套类型,从而在它们支持的类型定义中嵌套支持枚举,类和结构. ...
- swift语言点评四-Closure
总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...
随机推荐
- zeromq-4.1.2在windows下的编译
作者:朱金灿 来源:http://blog.csdn.net/clever101 zeromq是一个最近比较火的跨平台消息中间件,最近准备研究它,故下载它的源码编译了一下.我是使用VS2008编译的, ...
- ndis6 how to drop packets
In ndis6 how to drop packets? in FilterSendNetBufferLists: FILTER_RELEASE_LOCK(&pFilter->Lock ...
- c#中 abstract 和 virtual 的区别与用法
先来看abstract方法,顾名思义,abstract方法就是抽象方法. 1.抽象方法就是没有实现的,必须是形如: public abstract void Init(); 2.拥有抽象方法的类 ...
- Object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects&qu ...
- css3之BFC、IFC、GFC和FFC
CSS2.1中只有BFC和IFC, CSS3中才有GFC和FFC. What's FC?一定不是KFC,FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它 ...
- 路飞学城Python-Day50
05-运算符 常用运算符 算数运算符 赋值运算符 比较运算符 逻辑运算符 // 赋值运算符 var money = prompt('请输入金额'); ...
- CentOS7上安装google谷歌浏览器
1.首先进入根目录,然后进入etc/yum.repos.d目录下,创建google-chrome.repo文件 cd / cd etc/yum.repos.d vim google-chrome. ...
- Day 04 [与用户交互,格式化输出,基本运算符]
Python 的与用户交互 name=input("请输入姓名:") height=input('请输入身高:') weight=input('请输入体重:') 在python3中 ...
- 广义线性模型------逻辑回归和softmax回归
1.广义线性模型 2.逻辑回归 3.softmax回归
- faker smtp server
import os import asyncio import logging import base64 from email import message_from_bytes from emai ...