类与结构体的计算属性
@H_403_4@/*定义一个点*/
struct Point {
var x:Double!,y:Double!
}
@H_403_4@/*定义矩形的宽高*/
struct Size {
var width:Double!,height:Double!
}
@H_403_4@/*定义一个矩形*/
struct Rect {
var origin=Point()
var size=Size()
var center:Point{
get{
let centerX=origin.x+(size.width/2)
let centerY=origin.y+(size.height/2)
return Point(x: centerX,y: centerY)
}
set(newCenter){
origin.x=newCenter.x-(size.width/2)
origin.y=newCenter.y-(size.height/2)
}
}
}
var rectangular=Rect(origin: Point(x: 0,y:0),size: Size(width: 2,height: 2))
print(rectangular.center)
下标脚本
关键词subscript
/*下标脚本*/
struct TiMetable {
let multiplier:Int
subscript (index:Int)->Int{
return multiplier*index
}
}
let fourTiMetable=TiMetable(multiplier: 4)
print(fourTiMetable[8])//32