<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 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 ``` 實際上,`plus()`函數已重載以與各種 Kotlin 原始類型和`String`一起使用。 ```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()`函數來定義`+`運算符對對象的工作方式。 ### 示例:重載`+`運算符 ```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) } } ``` 運行該程序時,輸出為: ```kt sum = (5, 1) ``` 這里,`plus()`函數用`operator`關鍵字標記,以告知編譯器`+`運算符正在重載。 表達式`p1 + p2`在后臺被轉換為`p1.plus(p2)`。 * * * ### 示例:重載`-`運算符 在此示例中,您將學習重載`--`運算符。 表達式`--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) } ``` 盡管上面的程序在技術上是正確的,但我們使用`+`運算符減去了兩個對象的相應屬性,這使程序變得混亂。 2.與 Scala 之類的語言不同,Kotlin 中只能重載特定的[運算符集](https://kotlinlang.org/docs/reference/operator-overloading.html)。 訪問頁面以了解可在 Kotlin 中重載的運算符及其相應的成員函數。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看