定义坐标点类
class Point{
var x:Float!
var y:Float!
init(x:Float,y:Float){
self.x=x
self.y=y
}
}
重载加减运算符
func +(p1:Point,p2:Point) -> Point {
return Point(x: p1.x+p2.x,y: p1.y+p2.y)
}
func -(p1:Point,p2:Point) -> Point {
return Point(x: p1.x-p2.x,y: p1.y-p2.y)
}
测试案例
var p1=Point(x: 1,y: 2)
var p2=Point(x: 3,y: -5)
p1=p1+p2
print(p1.x,p1.y)
p1=p1-p2
print(p1.x,p1.y)
输出
4.0 -3.0
1.0 2.0