Kotlin operator方法的重载
Posted 安果移不动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kotlin operator方法的重载相关的知识,希望对你有一定的参考价值。
重写加法让他支持加法
data class Coordinate2(var x: Int, var y: Int)
operator fun plus(c2: Coordinate2) = Coordinate2(x + c2.x, y + c2.y)
fun main()
val c1 = Coordinate2(10, 20)
val c2 = Coordinate2(10, 20)
println(c1 + c2)
其他的一些重载写法
以上是关于Kotlin operator方法的重载的主要内容,如果未能解决你的问题,请参考以下文章
Kotlin常用的 Kotlin 类 ① ( 嵌套类 | 数据类 | 数据类 copy 函数 | 数据类解构声明 operator fun component1 | 数据类运算符重载 )