<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國際加速解決方案。 廣告
                在 Scala 中函數與方法通常都表示一個意思,二者在使用上沒有實質區別,一般在開發中也是可以相互替代和轉換的。 <br> 在函數式編程中,函數是一等公民。函數可以作為參數值傳入其他函數中,函數的返回值可以是函數,函數甚至可以嵌套函數。 <br/> 如果非要區分的話,總結的區別如下: (1)Scala 中的方法跟 Java 的類似,方法是組成類的一部分,指代類的成員; (2)Scala 中的函數則是一個完整的對象,其實就是繼承了Trait 的類的對象,Scala中內置了了許多 Trait(Function0~Function22)分別表示不同的參數個數的函數; (3)如果更明確劃分的話,Scala 中使用 `val` 語句定義函數,`def` 語句定義方法。(但我們習慣上將 `def` 定義稱為函數,`val`定義的為常量); ```scala object HelloWorld { def main(args: Array[String]): Unit = { // 定義方法 def m1(x: Int, y: Int): Int = x * y def m2(): Unit = println("無參方法") println(m1(100, 200)) // 20000 m2() // 無參方法 m2 // 無參時可以省略 () // 定義函數 val m3 = (x: Int, y: Int) => { x * y } val m4 = () => println("無參函數") println(m3(100, 200)) // 20000 m4() // 無參方法 // m4 即使函數無參,調用時也不能省略 (),否則什么都不做,也不報錯 } } ``` 通過反編譯,看到的Java代碼如下: ```java ... // m3是一個Function2對象 Function2 m3 = new AbstractFunction2.mcIII.sp() { public static final long serialVersionUID = 0L; ... }; // m4 是一個Function0對象 Function0 m4 = new HelloWorld..anonfun.2(); ... } // m1和m2是一個方法 private final int m1$1(int x, int y) { return x * y; } private final void m2$1() { Predef..MODULE$.println("無參方法"); } } ```
                  <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>

                              哎呀哎呀视频在线观看