基本运算符
运算符本质上就是一个函数
class Complex(val real:Double, var imaginary:Double){
operator fun plus(other:Complex):Complex{
return Complex(real + other.real, imaginary + other.imaginary)
}
override fun toString(): String {
return "$real + $imaginary i"
}
}
var c1 = Complex(3.0,4.0)
var c2 = Complex(5.0, 6.0)
println(c1 + c2)
输出为
8.0 + 10.0 i
运算符对应的函数 可以参考以下网址
http://www.kotlincn.net/docs/reference/operator-overloading.html
中缀表达式
infix
可以定义出类似in的写法
class Children(val name:String){
infix fun study(school: Any):Boolean{
return false
}
}
class XueXiao
val child:Children = Children("YuanJY")
val xueXiao:XueXiao = XueXiao()
if (child study xueXiao){
print("study in xuexiao")
}