定義坐標(biāo)點(diǎn)類
class Point{
var x:Float!
var y:Float!
init(x:Float,y:Float){
self.x=x
self.y=y
}
}
重載加減運(yùn)算符
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)
}
測(cè)試案例
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