swfit:运算符重载 Operator Methods
Operator Methods
Classes and structures can provide their own implementations of existing operators. This is known as overloading the existing operators.
- struct Vector2D {
- var x = 0.0, y = 0.0
- }
- extension Vector2D {
- static func + (left: Vector2D, right: Vector2D) -> Vector2D {
- return Vector2D(x: left.x + right.x, y: left.y + right.y)
- }
- }
The type method can be used as an infix operator between existing Vector2D instances:
- let vector = Vector2D(x: 3.0, y: 1.0)
- let anotherVector = Vector2D(x: 2.0, y: 4.0)
- 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 prefix, infix or postfix modifiers:
- 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:
- extension Vector2D {
- static prefix func +++ (vector: inout Vector2D) -> Vector2D {
- vector += vector
- return vector
- }
- }
- var toBeDoubled = Vector2D(x: 1.0, y: 4.0)
- let afterDoubling = +++toBeDoubled
- // toBeDoubled now has values of (2.0, 8.0)
- // afterDoubling also has values of (2.0, 8.0)
https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html
swfit:运算符重载 Operator Methods的更多相关文章
- c++ 运算符重载operator
一般格式为: 函数类型 operator 运算符名称(形参列表){ 对运算符的重载 } 注意函数名是由operator和运算符组成.在上面的一般格式中,operator是关键字,是专门用于重载运算符函 ...
- PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天
PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数 1 构造 2 析构 3 赋值 4 拷贝构造 5 oper ...
- 《Inside C#》笔记(十一) 运算符重载
运算符重载与之前的索引器类似,目的是为了让语言本身使用起来更方便直接,也是一种语法糖. 一 运算符重载(Operator Overloading) 运算符重载的存在,使得现有的各种运算符可以被重新定义 ...
- [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)
operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...
- C/C++对bool operator < (const p &a)const的认识,运算符重载详解(杂谈)
下面来进行这段代码的分析: struct node { //定义一个结构体node(节点) int x; int y; int len; //node中有3个成员变量x,y,l ...
- C++的拷贝构造函数、operator=运算符重载,深拷贝和浅拷贝、explicit关键字
原文地址:https://blog.csdn.net/shine_journey/article/details/53081523 1.在C++编码过程中,类的创建十分频繁. 简单的功能,当然不用考虑 ...
- 类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete
一.类型转换运算符 必须是成员函数,不能是友元函数 没有参数 不能指定返回类型 函数原型:operator 类型名(); C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 1 ...
- Pthon魔术方法(Magic Methods)-运算符重载
Pthon魔术方法(Magic Methods)-运算符重载 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Python运算符对应的魔术方法 1>.比较运算符 <: ...
- Java 原始数据类型的计算:运算符重载(Operator Overload)和类型转换(Type Conversion)
原文阅读:<算法(第四版)>第一章 第一节:基础编程模型 有没有在面试的时候被问到:下面这几行代码的执行结果是什么?依据是什么? System.out.println (5/3); Sys ...
随机推荐
- CodeForces-380C:Sereja and Brackets(线段树与括号序列)
Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...
- 「咕咕网校 - 基础省选」树上问题的进阶 by Drench
一定要在noip之前把自己花钱买的Luogu网课梳理完!QAQ 树上前缀和: 对于有根树,在每个点记录 val (点权) 和 sum(到根的点权之和) 当然记录的值因题而异(但是既然叫树上前缀和当然就 ...
- Java中gcRoot和引用类型
看到一个老问题,Java是如何判定回收哪些对象的? 答:从gcRoot根搜索不可达,且标记清理一次之后仍没有被复活的对象,会被认定为垃圾对象进行清理.注意在Java中没有对象的作用域,只有对象的引用的 ...
- 使用webpack报错
意思是: 意思是CLI被移动到了一个专门的包 webpack-cli里了.请安装webpack-cli 的除了webpack本身使用cli当用npm时,使用npm install webpack-cl ...
- HTML 新属性
accesskey 属性 -- 代表链接的快捷键访问方式 定义了accesskey的链接可以使用快捷键(ALT+字母)访问. 主菜单与导航菜单使用accesskey,通常是不错的选择. 取值: 字母 ...
- linux以行为单位进行读写操作
1 gets/fgets函数 char* fgets(char *restrict buf,int n,FILE *restrict fp) 参数1:存放读入串的缓冲区 参数2:表示读入的字符个数,最 ...
- bzoj 3697: 采药人的路径【点分治】
点分治,设当前处理的块的重心为rt,预处理出每个子树中f[v][0/1]表示组合出.没组合出一对值v的链数(从当前儿子出发的链),能组合出一对v值就是可以有一个休息点 然后对于rt,经过rt且合法的路 ...
- 洛谷P3825 [NOI2017]游戏(2-SAT)
传送门 果然图论的题永远建图最麻烦……看着题解代码的建图过程真的很珂怕…… 先不考虑地图$x$,那么每一个地图都只能用两种赛车,于是我们可以用2-SAT来搞,用$i$表示这个地图能用的第一辆车,$i' ...
- mysql 分区后查询效率
准备工作: 蠕虫复制 文章表 增加数据到112万 语法:insert into tableNameA select * from tableNameB 未分区查询 54s 改变现有表 ...
- Python基础:一起来面向对象 (二) 之搜索引擎
实例 搜索引擎 一个搜索引擎由搜索器.索引器.检索器和用户接口四个部分组成 搜索器就是爬虫(scrawler),爬出的内容送给索引器生成索引(Index)存储在内部数据库.用户通过用户接口发出询问(q ...