Swift - 自定义运算符
2022-04-03 本文已影响0人
aven_kang
precedencegroup PlusMinusPrecedence {
associativity: none
higherThan: AdditionPrecedence
lowerThan: MultiplicationPrecedence
assignment: true
}
infix operator +- : PlusMinusPrecedence
struct Point {
var x = 0, y = 0
static func +- (p1:Point,p2:Point) -> Point {
Point(x: p1.x+p2.x, y: p1.y - p2.y)
}
}
上述代码中,我们自定义了一个运算符,对Point的x相加,对Point的y进行相减
var p1 = Point(x: 10, y: 10)
var p2 = Point(x: 15, y: 20)
var p3 = p2 +- p1