Operator Methods

Classes and structures can provide their own implementations of existing operators. This is known as overloading the existing operators.

  1. struct Vector2D {
  2. var x = 0.0, y = 0.0
  3. }
  4. extension Vector2D {
  5. static func + (left: Vector2D, right: Vector2D) -> Vector2D {
  6. return Vector2D(x: left.x + right.x, y: left.y + right.y)
  7. }
  8. }

The type method can be used as an infix operator between existing Vector2D instances:

  1. let vector = Vector2D(x: 3.0, y: 1.0)
  2. let anotherVector = Vector2D(x: 2.0, y: 4.0)
  3. let combinedVector = vector + anotherVector

Prefix and Postfix Operators

The example shown above demonstrates a custom implementation of a binary infix operator. Classes and structures can also provide implementations of the standard unary operators. Unary operators operate on a single target. They are prefix if they precede their target (such as -a) and postfix operators if they follow their target (such as b!).

Custom Operators

You can declare and implement your own custom operators in addition to the standard operators provided by Swift. For a list of characters that can be used to define custom operators, see Operators.

New operators are declared at a global level using the operator keyword, and are marked with the prefixinfix or postfix modifiers:

  1. prefix operator +++

The example above defines a new prefix operator called +++. This operator does not have an existing meaning in Swift, and so it is given its own custom meaning below in the specific context of working with Vector2D instances. For the purposes of this example, +++ is treated as a new “prefix doubling” operator. It doubles the x and y values of a Vector2D instance, by adding the vector to itself with the addition assignment operator defined earlier. To implement the +++ operator, you add a type method called +++ to Vector2D as follows:

  1. extension Vector2D {
  2. static prefix func +++ (vector: inout Vector2D) -> Vector2D {
  3. vector += vector
  4. return vector
  5. }
  6. }
  7. var toBeDoubled = Vector2D(x: 1.0, y: 4.0)
  8. let afterDoubling = +++toBeDoubled
  9. // toBeDoubled now has values of (2.0, 8.0)
  10. // afterDoubling also has values of (2.0, 8.0)

https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html

swfit:运算符重载 Operator Methods的更多相关文章

  1. c++ 运算符重载operator

    一般格式为: 函数类型 operator 运算符名称(形参列表){ 对运算符的重载 } 注意函数名是由operator和运算符组成.在上面的一般格式中,operator是关键字,是专门用于重载运算符函 ...

  2. PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天

    PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数    1  构造  2  析构   3  赋值  4 拷贝构造  5 oper ...

  3. 《Inside C#》笔记(十一) 运算符重载

    运算符重载与之前的索引器类似,目的是为了让语言本身使用起来更方便直接,也是一种语法糖. 一 运算符重载(Operator Overloading) 运算符重载的存在,使得现有的各种运算符可以被重新定义 ...

  4. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)

    operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...

  5. C/C++对bool operator < (const p &a)const的认识,运算符重载详解(杂谈)

    下面来进行这段代码的分析: struct node {  //定义一个结构体node(节点)    int x;    int y;    int len;   //node中有3个成员变量x,y,l ...

  6. C++的拷贝构造函数、operator=运算符重载,深拷贝和浅拷贝、explicit关键字

    原文地址:https://blog.csdn.net/shine_journey/article/details/53081523 1.在C++编码过程中,类的创建十分频繁. 简单的功能,当然不用考虑 ...

  7. 类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete

    一.类型转换运算符 必须是成员函数,不能是友元函数 没有参数 不能指定返回类型 函数原型:operator 类型名();  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  8. Pthon魔术方法(Magic Methods)-运算符重载

    Pthon魔术方法(Magic Methods)-运算符重载 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Python运算符对应的魔术方法 1>.比较运算符 <: ...

  9. Java 原始数据类型的计算:运算符重载(Operator Overload)和类型转换(Type Conversion)

    原文阅读:<算法(第四版)>第一章 第一节:基础编程模型 有没有在面试的时候被问到:下面这几行代码的执行结果是什么?依据是什么? System.out.println (5/3); Sys ...

随机推荐

  1. 【转】IntelliJ IDEA搭建Spring环境

    //本来在草稿箱写好了,忘记发就被冲掉了,重新再写一遍. Spring初探 Spring初探 在IntelliJ IDEA中创建Spring项目 一个简单的例子介绍框架的作用 那么什么时候new的对象 ...

  2. 通过配置Mysql参数提高写入速度(整理)

    1) innodb_buffer_pool_size 如果用Innodb,那么这是一个重要变量.相对于MyISAM来说,Innodb对于buffer size更敏感.MySIAM可能对于大数据量使用默 ...

  3. pymssql读取varchar字段中文显示乱码的问题分析

    问题 用python的pymssql模块读取旧业务系统后台SQL Server 2000数据库展示数据为乱码 开发环境 操作系统:windows 8 数据库 MS SQL Server 2000,默认 ...

  4. NYOJ7——街区最短路径问题

    街区最短路径问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4  描述:一个街区有很多住户,街区的街道只能为东西.南北两种方向.住户只可以沿着街道行走.各个街道之间的间隔相等 ...

  5. JAVA基础--JAVA API常见对象(字符串&缓冲区)11

    一. String 类型 1. String类引入 第二天学习过Java中的常量:   常量的分类:   数值型常量:整数,小数(浮点数) 字符型常量:使用单引号引用的数据 字符串常量:使用双引号引用 ...

  6. firefly安装步骤

    本来公司一个网游服务器端选定了pomelo框架,后来出了个Firefly,为做一个对比,决定研究一下Firefly.看了一下Firefly,感觉头大,python的,本人python小白,只好慢慢折腾 ...

  7. HDU 1230饭前开胃菜

    题意不讲了.. 没思路,上去就是干.... 两个所谓要加的数直接存到数组,开一个标记的数组,然后直接加,乱搞一波,就好了. 细心一点. #include<iostream> #includ ...

  8. MySQL 使用 MySQLDump 复制数据库

    1.导出整个数据库  mysqldump -u 用户名 -p 数据库名 > 导出的文件名      mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.s ...

  9. IT兄弟连 Java Web教程 Web开发的相关知识

    Web基本概念 Web,是环球信息网的缩写,也称作“WWW.W3”,英文全称为World Wide Web,中文名成为万维网,常简称为Web.Web分为Web客户端和Web服务器程序.Web可以让We ...

  10. childNodes和Children的区别

    1.childNodes: 标准的,返回指定元素的子元素集合,包括HTML属性,所有属性,文本.可以通过nodeType来判断是哪种类型的节点,只有当nodeType==1时才是元素节点,2是属性节点 ...