kotlin 重写运算符

  1. 重载 + 运算符

假设有一个表示点的类,可以重载+运算符来实现两个点的相加:

data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point): Point {
        return Point(x + other.x, y + other.y)
    }
}

fun main() {
    val point1 = Point(2, 3)
    val point2 = Point(4, 5)
    val result = point1 + point2
    println(result)  // 输出: Point(x=6, y=8)
}
  1. 重载 - 运算符

类似的,可以重载-运算符来实现两个点的相减:

data class Point(val x: Int, val y: Int) {
    operator fun minus(other: Point): Point {
        return Point(x - other.x, y - other.y)
    }
}

fun main() {
    val point1 = Point(10, 5)
    val point2 = Point(3, 2)
    val result = point1 - point2
    println(result)  // 输出: Point(x=7, y=3)
}
  1. 重载 * 运算符

也可以重载乘法运算符*来对数值进行乘法操作:

data class Point(val x: Int, val y: Int) {
    operator fun times(scale: Int): Point {
        return Point(x * scale, y * scale)
    }
}

fun main() {
    val point = Point(3, 4)
    val result = point * 2
    println(result)  // 输出: Point(x=6, y=8)
}
  1. 重载 [] 运算符

还可以重载[]运算符来实现访问类内部数据的功能:

class Matrix(val rows: Int, val cols: Int) {
    private val data = Array(rows) { IntArray(cols) }

    operator fun get(i: Int, j: Int): Int {
        return data[i][j]
    }

    operator fun set(i: Int, j: Int, value: Int) {
        data[i][j] = value
    }
}

fun main() {
    val matrix = Matrix(2, 2)
    matrix[0, 0] = 1
    matrix[0, 1] = 2
    matrix[1, 0] = 3
    matrix[1, 1] = 4

    println(matrix[0, 0])  // 输出: 1
    println(matrix[1, 1])  // 输出: 4
}
  1. 实现迭代器方法

假设有一个自定义的集合类MyCollection,可以实现Iterator接口来为其提供迭代功能。

class MyCollection<T>(private val items: List<T>) : Iterable<T> {
    override fun iterator(): Iterator<T> {
        return MyIterator(items)
    }

    class MyIterator<T>(private val items: List<T>) : Iterator<T> {
        private var index = 0

        override fun hasNext(): Boolean {
            return index < items.size
        }

        override fun next(): T {
            if (!hasNext()) throw NoSuchElementException()
            return items[index++]
        }
    }
}

fun main() {
    val myCollection = MyCollection(listOf(1, 2, 3, 4))
    for (item in myCollection) {
        println(item)  // 输出: 1 2 3 4
    }
}
  1. 重写==运算符和equals方法

在Kotlin中,==运算符的比较实际上是调用equals方法。因此,只需要重写equals方法即可,同时建议重写hashCode方法以保证一致性。以下是如何重写equals和hashCode的示例:

data class Person(val name: String, val age: Int) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is Person) return false

        return name == other.name && age == other.age
    }

    override fun hashCode(): Int {
        var result = name.hashCode()
        result = 31 * result + age
        return result
    }
}

fun main() {
    val person1 = Person("Alice", 30)
    val person2 = Person("Alice", 30)
    val person3 = Person("Bob", 25)

    println(person1 == person2)  // 输出: true
    println(person1 == person3)  // 输出: false
}
  1. 重写==运算符的简化方式

如果使用data class,Kotlin会自动为生成equals和hashCode方法。不需要手动重写它们。

data class Person(val name: String, val age: Int)

fun main() {
    val person1 = Person("Alice", 30)
    val person2 = Person("Alice", 30)
    val person3 = Person("Bob", 25)

    println(person1 == person2)  // 输出: true
    println(person1 == person3)  // 输出: false
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值