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 ...
随机推荐
- CoreOS,CoreOS,一款 Linux 容器发行版
CoreOS,一款最新的 Linux 发行版本,支持自动升级内核软件,提供各集群间配置的完全控制. 关于使用哪个版本的 Linux 服务器系统的争论,常常是以这样的话题开始的: 你是喜欢基于 Red ...
- 读取每行的数据,加入到list中
有txt文件中,每行都有一个字符串或者数据,将每行的数据转换到一个list中 例如: 1 2 3 6 实现: f = open("test1.txt",'r') list1 = [ ...
- 【重要】Selenium2+python自动化44-元素定位参数化(find_element)
转:https://www.cnblogs.com/yoyoketang/p/6551274.html 前言 元素定位有八种方法,这个能看到这一篇的小伙伴都知道了,那么有没有一种方法,可以把八种定位合 ...
- DB Link 去除域名
1.查看global_name的设置 SQL> show parameters global_name; NAME TYPE ...
- E20180402-hm
cascade n. 串联; 倾泻; 小瀑布,瀑布状物; restrict vt. 限制,限定; 约束,束缚; strict adj. 精确的; 绝对的; 严格的,严谨的; [植] 笔直的; re ...
- Mac下的常用终端命令与vim常用命令
因为很少用命令行,老被鄙视,所以今天记录一下常用的命令行: cd 切换工作目录 . 表示当前目录 .. 表示当前目录的上一级目录 / 根目录/目录分隔符 ./ 当前目录 ../ 回到上一级目录 ls ...
- poj2421【MST-prim+Kruskal】
水过~~~~打好基础/~~ ------prim #include <iostream> #include <stdio.h> #include <string.h> ...
- (数位DP)51NOD 1042 数字0-9的数量
给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次. 输入 ...
- LuoguP3964 [TJOI2013]松鼠聚会【切比雪夫距离/前缀和】
题目传送门 前置知识:切比雪夫距离和曼哈顿距离的相互转化--自为风月马前卒 有了这个知识,我们便可以在读入松鼠的家的坐标时,先把他转化一下,然后把最后的总式化简,我们会得到一个充满后缀和以及前缀和的式 ...
- NOIp 2014 解方程 【数学/秦九韶算法/大数取膜】By cellur925
题目传送门 题意:求高次方程的解及其个数.其中 1° 我们知道,高次方程是没有求根公式的.但是利用逆向思维,我们可以进行“试根法”,因为题目中给出了所求根的范围.但是多项式系数过于吓人,达到了sxbk ...