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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Kotlin 默認和命名參數 > 原文: [https://www.programiz.com/kotlin-programming/default-named-arguments](https://www.programiz.com/kotlin-programming/default-named-arguments) #### 在本文中,您將在示例的幫助下了解默認參數和命名參數。 ## Kotlin 默認參數 在 Kotlin 中,您可以為函數定義中的參數提供默認值。 如果通過傳遞的參數調用該函數,則將這些參數用作參數。 但是,如果在不傳遞參數的情況下調用該函數,則會使用默認參數。 * * * ### 默認參數如何工作? **情況 I:所有參數均傳遞** * * * ![Both arguments passed to the function](https://img.kancloud.cn/68/90/6890ea39f22b1de7f77a5940afad0d14_405x159.png) 函數`foo()`具有兩個參數。 參數提供有默認值。 但是,通過在上述程序中傳遞兩個參數來調用`foo()`。 因此,不使用默認參數。 在`foo()`函數內,`letter`和`num`的值分別為`'x'`和`2`。 **情況 II:未傳遞所有參數** * * * ![All arguments are not passed to the function](https://img.kancloud.cn/8b/d8/8bd8597813f576258ed2606250ae8ed5_398x156.png) 在此,僅一個(第一個)參數傳遞給`foo()`函數。 因此,第一個參數使用傳遞給函數的值。 但是,第二個參數`num`將采用默認值,因為在函數調用期間未傳遞第二個參數。 在`foo()`函數內,`letter`和`num`的值分別為`'y'`和`15`。 **情況 III:不傳遞任何參數** * * * ![No arguments passed to the function](https://img.kancloud.cn/2f/93/2f93896ced863012eb6efd2a92f584a2_392x154.png) 在此,不傳遞任何參數就調用`foo()`函數。 因此,兩個參數都使用其默認值。 在`foo()`函數內,`letter`和`num`的值分別為`'a'`和`15`。 * * * ### 示例:Kotlin 默認參數 ```kt fun displayBorder(character: Char = '=', length: Int = 15) { for (i in 1..length) { print(character) } } fun main(args: Array<String>) { println("Output when no argument is passed:") displayBorder() println("\n\n'*' is used as a first argument.") println("Output when first argument is passed:") displayBorder('*') println("\n\n'*' is used as a first argument.") println("5 is used as a second argument.") println("Output when both arguments are passed:") displayBorder('*', 5) } ``` 運行該程序時,輸出為: ```kt Output when no argument is passed: =============== '*' is used as a first argument. Output when first argument is passed: *************** '*' is used as a first argument. 5 is used as a second argument. Output when both arguments are passed: ***** ``` * * * ## Kotlin 命名參數 在討論命名參數之前,讓我們考慮對上面的代碼進行一些修改: ```kt fun displayBorder(character: Char = '=', length: Int = 15) { for (i in 1..length) { print(character) } } fun main(args: Array<String>) { displayBorder(5) } ``` 在這里,我們試圖將第二個參數傳遞給`displayBorder()`函數,并使用默認參數作為第一個參數。 但是,此代碼將提供使用錯誤。 這是因為編譯器認為我們正在嘗試為字符(`Char`類型)提供 5(`Int`值)。 為了解決這種情況,可以使用命名參數。 方法如下: * * * ### 示例:Kotlin 命名參數 ```kt fun displayBorder(character: Char = '=', length: Int = 15) { for (i in 1..length) { print(character) } } fun main(args: Array<String>) { displayBorder(length = 5) } ``` 運行該程序時,輸出為: ```kt ===== ``` 在上面的程序中,我們使用命名參數(`length = 5`)指定函數定義中的`length`參數應采用此值(與參數的位置無關)。 ![Named Arguments in Kotlin](https://img.kancloud.cn/2e/c2/2ec2158a2352707ae08b50267525e4df_527x190.png) 第一個參數`character`使用程序中的默認值`'='`。
                  <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>

                              哎呀哎呀视频在线观看