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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 1. 函數的定義 # 1.1. 函數定義語法 函數的語法格式具體如下: ``` fun 函數名稱(參數名稱: 參數類型, ...): 返回值類型{ return 返回值 } ``` 比如: ~~~ fun add(numberA: Int, numberB: Int): Int{ return numberA + numberB } ~~~ 注意到這里的函數體并沒有什么其余的部分,也就是其實就執行了一個表達之`numberA+numberB`,可以將上面的函數簡寫為: ~~~ fun add(A: Int, B: Int) = A + B ~~~ 對于這樣沒有指定返回值類型的函數,編輯器會根據函數的返回值進行推斷。 # 1.2. 默認參數 類似于`Python`中的寫法,可以在定義函數的時候指定默認參數,比如下面的案例: ~~~ fun getUserName(name: String = "張三"):String = name fun main() { println(getUserName()) println(getUserName("李四")) } ~~~ 結果為: ``` 張三 李四 ``` # 1.3. 可變參數 在`Java`和`Python`中均有可變參數的存在,可變參數,是指參數類型確定但個數不確定的參數,可變參數通過`vararg`關鍵字標識,我們可以將其理解為數組。**可變參數通常聲明在形參列表中的最后位置**,如果不聲明在最后位置,那么可變參數后面的其他參數都需要通過命名參數的形式進行傳遞。比如下面的案例: ~~~ fun sum(vararg number: Int):Int{ var count = 0 for (i in number){ count += i } return count } fun main() { println(sum()) println(sum(1, 2, 3, 4, 5, 6, 7, 8, 9)) } ~~~ 結果為: ``` 0 45 ``` # 2. 局部函數 在`Kotlin`中支持函數的嵌套,其用法類似在`Python`中的用法。比如下面的案例: ~~~ fun sum(vararg number: Int):Int{ fun localSum(a: Int, b: Int) = a + b var count = 0 for (i in number){ count = localSum(count, i) } return count } ~~~ # 3. 擴展函數 擴展函數也就是在原本類的基礎上增加一些新的方法。比如下面的案例: ~~~ fun String.doSomething(count: Int = 1): String{ return this.repeat(count) } fun Any.showInfo(): Any{ println(this) return this; } fun main() { "abc".showInfo() "abc".doSomething(2).showInfo() } ~~~ 結果: ``` abc abcabc ``` # 4. 當二個參數為函數 比如下面的案例: ~~~ fun doTest(data: Int?, other: (a: String) -> String): String{ return other(data.toString()) } fun main() { // 將第二個函數參數作為最后的括號內的表達式 val doTest = doTest(123) { it + "3" } println(doTest) } ~~~ 結果: ``` 1233 ```
                  <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>

                              哎呀哎呀视频在线观看