Overloading of Kotlin operator methods

Rewrite addition to make it support addition

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)
}

some other overloading

Guess you like

Origin blog.csdn.net/mp624183768/article/details/123894791