0
点赞
收藏
分享

微信扫一扫

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方法的重载_android

其他的一些重载写法

Kotlin operator方法的重载_kotlin_02

举报

相关推荐

0 条评论