Scala中Method方法和Function函數的區別
2 回答

慕桂英4014372
TA貢獻1871條經驗 獲得超13個贊
scala> //定義一個方法
scala> def m(x:Int) = 2*x
m: (x: Int)Int
scala> //定義一個函數
scala> val f = (x:Int) => 2*x
f: Int => Int = <function1>
scala> //方法不能作為最終表達式出現
scala> m
<console>:9: error: missing arguments for method m;
follow this method with `_‘ if you want to treat it as a partially applied function

手掌心
TA貢獻1942條經驗 獲得超3個贊
函數是一個對象,繼承自FuctionN,函數對象有curried,equals,isInstanceOf,toString這些方法。
而方法不具有這些特性。如果想把方法轉換成一個函數,可以用方法名跟上下劃線的方式.
- 2 回答
- 0 關注
- 805 瀏覽
添加回答
舉報
0/150
提交
取消