36.md 3.2 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# Kotlin 运算符重载

> 原文: [https://www.programiz.com/kotlin-programming/operator-overloading](https://www.programiz.com/kotlin-programming/operator-overloading)

#### 在本文中,您将通过示例学习有关运算符重载(定义运算符如何处理用户定义的类型(如对象)的方法)。

在 Kotlin 中使用运算符时,将调用其相应的成员函数。 例如,表达式`a+b`在后台转换为`a.plus(b)`

```kt
fun main(args: Array<String>) {
    val a = 5
    val b = 10

    print(a.plus(b)) // print(a+b)
}
```

运行该程序时,输出为:

```kt
15
```

W
wizardforcel 已提交
24
实际上,`plus()`函数已重载以与各种 Kotlin 原始类型和`String`一起使用。
W
wizardforcel 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

```kt
// + operator for basic types
operator fun plus(other: Byte): Int
operator fun plus(other: Short): Int
operator fun plus(other: Int): Int
operator fun plus(other: Long): Long
operator fun plus(other: Float): Float
operator fun plus(other: Double): Double

// for string concatenation
operator fun String?.plus(other: Any?): String

```

* * *

您还可以通过重载对象的相应功能来定义操作符对对象的工作方式。 例如,您需要通过重载`plus()`函数来定义`+`运算符对对象的工作方式。

W
wizardforcel 已提交
44
### 示例:重载`+`运算符
W
wizardforcel 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

```kt
fun main(args: Array<String>) {
    val p1 = Point(3, -8)
    val p2 = Point(2, 9)

    var sum = Point()
    sum = p1 + p2

    println("sum = (${sum.x}, ${sum.y})")
}

class Point(val x: Int = 0, val y: Int = 10) {

    // overloading plus function
    operator fun plus(p: Point) : Point {
        return Point(x + p.x, y + p.y)
    }
}
```

When you run the program, the output will be:

```kt
sum = (5, 1)
```

这里,`plus()`函数用`operator`关键字标记,以告知编译器`+`运算符正在重载。

表达式`p1 + p2`在后台被转换为`p1.plus(p2)`

* * *

W
wizardforcel 已提交
78
### 示例:重载`-`运算符
W
wizardforcel 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

在此示例中,您将学习重载`--`运算符。 表达式`--a`在引擎盖下转换为`a.dec()`

`dec()`成员函数不带任何参数。

```kt
fun main(args: Array<String>) {
    var point = Point(3, -8)
    --point

    println("point = (${point.x}, ${point.y})")
}

class Point(var x: Int = 0, var y: Int = 10) {
    operator fun dec() = Point(--x, --y)
}
```

运行该程序时,输出将是:

```kt
point = (2, -9)

```

* * *

请记住,

```kt
operator fun dec() = Point(--x, --y)
```

相当于

```kt
operator fun dec(): Point {
    return Point(--x, --y)
}
```

* * *

### 几个要点

1.重载运算符时,应尝试保持运算符的原始精神。 例如,

```kt
fun main(args: Array<String>) {
    val p1 = Point(3, -8)
    val p2 = Point(2, 9)

    var sum = Point()
    sum = p1 + p2

    println("sum = (${sum.x}, ${sum.y})")
}

class Point(val x: Int = 0, val y: Int = 10) {

    // overloading plus function
    operator fun plus(p: Point) = Point(x - p.x, y - p.y)
}
```

尽管上面的程序在技术上是正确的,但我们使用`+`运算符减去了两个对象的相应属性,这使程序变得混乱。

W
wizardforcel 已提交
146
2.与 Scala 之类的语言不同,Kotlin 中只能重载特定的[运算符集](https://kotlinlang.org/docs/reference/operator-overloading.html)。 访问页面以了解可在 Kotlin 中重载的运算符及其相应的成员函数。