operator
关键字的函数。
通过添加 operator
修饰符,您可以允许函数使用者编写惯用的 Kotlin 代码。
示例:
class Complex(val real: Double, val imaginary: Double) {
fun plus(other: Complex) =
Complex(real + other.real, imaginary + other.imaginary)
}
fun usage(a: Complex, b: Complex) {
a.plus(b)
}
该快速修复会添加 operator
修饰符关键字:
class Complex(val real: Double, val imaginary: Double) {
operator fun plus(other: Complex) =
Complex(real + other.real, imaginary + other.imaginary)
}
fun usage(a: Complex, b: Complex) {
a + b
}