定义有什么,及哪些必须实现。

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.

  1. protocol SomeProtocol {
  2. var mustBeSettable: Int { get set }
  3. var doesNotNeedToBeSettable: Int { get }
  4. }

If you mark a protocol instance method requirement as mutating, you don’t need to write the mutatingkeyword 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 AnyObjectprotocol to a protocol’s inheritance list.

  1. protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
  2. // class-only protocol definition goes here

协议联合体作为参量

  1. func wishHappyBirthday(to celebrator: Named & Aged) {
  2. print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
  3. }
  4. let birthdayPerson = Person(name: "Malcolm", age: 21)
  5. wishHappyBirthday(to: birthdayPerson)

swift语言点评二十一-协议的更多相关文章

  1. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  2. swift语言点评二十-扩展

    结论:扩展无法修改原来的数据结构. Extensions can add new functionality to a type, but they cannot override existing ...

  3. Swift语言指南(二)--语言基础之注释和分号

    原文:Swift语言指南(二)--语言基础之注释和分号 注释 通过注释向自己的代码中注入不可执行的文本,作为你自己的笔记或提示.Swift编译器运行时会忽略注释. Swift的注释与C语言极其相似,单 ...

  4. Swift 学习之二十一:?和 !(详解)

    http://blog.csdn.net/woaifen3344/article/details/30244201 Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始 ...

  5. swift语言点评十二-Subscripts

    Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the m ...

  6. swift语言点评十一-Methods

    Assigning to self Within a Mutating Method Mutating methods can assign an entirely new instance to t ...

  7. 苹果新的编程语言 Swift 语言进阶(十一)--实例的初始化与类的析构

    一 .实例的初始化          实例的初始化是准备一个类.结构或枚举的实例以便使用的过程.初始化包括设置一个实例的每一个存储属性为一个初始值,以及执行任何其它新的实例能够使用之前需要的设置或初始 ...

  8. Swift5 语言指南(二十一) 嵌套类型

    通常创建枚举以支持特定类或结构的功能.类似地,定义纯粹在更复杂类型的上下文中使用的实用程序类和结构可能是方便的.为此,Swift允许您定义嵌套类型,从而在它们支持的类型定义中嵌套支持枚举,类和结构. ...

  9. swift语言点评四-Closure

    总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...

随机推荐

  1. zeromq-4.1.2在windows下的编译

    作者:朱金灿 来源:http://blog.csdn.net/clever101 zeromq是一个最近比较火的跨平台消息中间件,最近准备研究它,故下载它的源码编译了一下.我是使用VS2008编译的, ...

  2. ndis6 how to drop packets

    In ndis6 how to drop packets? in FilterSendNetBufferLists: FILTER_RELEASE_LOCK(&pFilter->Lock ...

  3. c#中 abstract 和 virtual 的区别与用法

    先来看abstract方法,顾名思义,abstract方法就是抽象方法. 1.抽象方法就是没有实现的,必须是形如:  public abstract void Init();   2.拥有抽象方法的类 ...

  4. Object-oriented programming

    Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects&qu ...

  5. css3之BFC、IFC、GFC和FFC

    CSS2.1中只有BFC和IFC, CSS3中才有GFC和FFC. What's FC?一定不是KFC,FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它 ...

  6. 路飞学城Python-Day50

    05-运算符 常用运算符 算数运算符 赋值运算符 比较运算符 逻辑运算符         // 赋值运算符          var money = prompt('请输入金额');          ...

  7. CentOS7上安装google谷歌浏览器

      1.首先进入根目录,然后进入etc/yum.repos.d目录下,创建google-chrome.repo文件 cd / cd etc/yum.repos.d vim google-chrome. ...

  8. Day 04 [与用户交互,格式化输出,基本运算符]

    Python 的与用户交互 name=input("请输入姓名:") height=input('请输入身高:') weight=input('请输入体重:') 在python3中 ...

  9. 广义线性模型------逻辑回归和softmax回归

    1.广义线性模型 2.逻辑回归 3.softmax回归

  10. faker smtp server

    import os import asyncio import logging import base64 from email import message_from_bytes from emai ...