自定义运算符
【示例1】:一元运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| struct Vector2D { var x = 0.0,y = 0.0 } // 声明一元运算符 prefix operator +++
// 扩展 extension Vector2D { static prefix func +++ (vector: Vector2D) -> Vector2D { return Vector2D(x: vector.x + vector.x,y: vector.y + vector.y) } } let vectorToDouble = Vector2D(x: 1.0,y: 2.0) let doubleVector = +++vectorToDouble print("\(doubleVector.x) \(doubleVector.y)")
|
编译运行,查看打印结果
【示例2】:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| // 中缀运算符 struct Vector2D { var x = 0.0,y = 0.0 } infix operator +-: AdditionPrecedence extension Vector2D { static func +- (left: Vector2D,right: Vector2D) -> Vector2D { return Vector2D(x: left.x + right.x,y: left.y - right.y) } } let firstVector = Vector2D(x: 1.0,y: 3.0) let secondVector = Vector2D(x: 2.0,y: 5.0) let result = firstVector +- secondVector print("\(result.x) \(result.y)")
|
编译运行,查看打印结果:
【示例3】:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| // 中缀运算符 struct Vector2D { var x = 0.0,y = 0.0 } infix operator *^: MultiplicationPrecedence extension Vector2D { static func *^ (left: Vector2D,right: Vector2D) -> Vector2D { return Vector2D(x: left.x * right.x,y: left.y * left.y + right.y * right.y) } } let firstVector = Vector2D(x: 1.0,y: 3.0) let secondVector = Vector2D(x: 2.0,y: 5.0) let result = firstVector *^ secondVector print("\(result.x) \(result.y)")
|
编译运行,查看打印结果:
自定义中缀运算符的优先级和结合性
【示例】:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| // 优先级和结合性 struct Vector2D { var x = 0.0,y = 0.0 } infix operator +-: AdditionPrecedence extension Vector2D { static func +- (left: Vector2D,right: Vector2D) -> Vector2D { return Vector2D(x: left.x + right.x,y: left.y - right.y) } } infix operator *^: MultiplicationPrecedence extension Vector2D { static func *^ (left: Vector2D,right: Vector2D) -> Vector2D { return Vector2D(x: left.x * right.x,y: left.y * left.y + right.y * right.y) } } let firstVector = Vector2D(x: 1.0,y: 2.0) let secondVector = Vector2D(x: 2.0,y: 3.0) let thirdVector = Vector2D(x: 3.0,y: 4.0) let result = firstVector +- secondVector *^ thirdVector print("\(result.x) \(result.y)")
|
编译运行,查看打印结果:
【注】:先计算 *^
优先级组,再计算 +-
优先级组
x: 2 * 3 = 6 + 1 = 7.0
y: 3 * 3 + 4 * 4 = 2 - 25 = -23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| // 自定义优先级和结合性 struct Vector2D { var x = 0.0,y = 0.0 } infix operator +-: AdditionPrecedence extension Vector2D { static func +- (left: Vector2D,right: Vector2D) -> Vector2D { return Vector2D(x: left.x + right.x,y: left.y - right.y) } } infix operator *^: MyPrecedencegroup
// 我的优先级组 precedencegroup MyPrecedencegroup { // 结合性 associativity: left // 优先级 lowerThan: AdditionPrecedence }
extension Vector2D { static func *^ (left: Vector2D,right: Vector2D) -> Vector2D { return Vector2D(x: left.x * right.x,y: left.y * left.y + right.y * right.y) } } let firstVector = Vector2D(x: 1.0,y: 2.0) let secondVector = Vector2D(x: 2.0,y: 3.0) let thirdVector = Vector2D(x: 3.0,y: 4.0) let result = firstVector +- secondVector *^ thirdVector print
|
编译运行,查看打印结果:
【注】:先计算 *^
优先级组,再计算 +-
优先级组
x:1 + 2 = 3 * 3 = 9.0
y:2 - 3 = -1 * -1 + 4 * 4 = 17.0