📅  最后修改于: 2021-01-01 14:37:54             🧑  作者: Mango
运算符重载是一种方法,您可以根据其自定义需要重新定义运算符行为。您可以重载所有算术运算运算符。运算符必须定义为静态。所有一元运算运算符必须使用〜(平铺)运算符表示这是一元运算运算符。
type OperatorOverloading(x: int, y : int) =
member this.X = x
member this.Y = y
static member (+) (v : OperatorOverloading, a) =
OperatorOverloading(a + v.X, a + v.Y)
override this.ToString() =
this.X.ToString() + " " + this.Y.ToString()
let operatorOverloading = OperatorOverloading(5, 3)
let operatorOverloading2 = operatorOverloading + 10
printfn "%s" (operatorOverloading.ToString())
printfn "%s" (operatorOverloading2.ToString())
输出:
5 3
15 13