类定义
@<class-name> {
property: Type
method(param: Type) {
# method body
this.property = ... # 类方法中可使用 this 获取当前示例
}
}
匿名类:
variable = ${
property: Type
new(param: Type) {
# constructor
}
method(param: Type) {
# method body
}
}
类实例化:
instance = <class-name>(param) # calls constructor
instance = <class-name> { property: <value> }
instance = <class-name> {
property1: <value1>
property2: <value2>
property3: <value3>
}
使用示例:
@Point {
x: Number
y: Number
distanceFromOrigin() {
return math.sqrt(x ^ 2 + y ^ 2)
}
}
point1 = Point(1, 1)
point2 = Point{ x: 1, y: 1 }