在 Scala 中, 不存在像 Java 單純運算符的設計, 取而代之的是運算符方法(函數), 進而達到能俱 運算符 Overloading 的特性, 一切的「操作」都是函數的調用呼叫. - WisdomFish.ORG 方法定義的語法似乎比較有趣,當它使用 = 操作符時,就像將隨後的方法體賦值給 main 標識符。事實上,真正發生的事情是: 在函數語言中,就像變數和常數一樣,函數是一級概念,所以語法上也是一樣地處理。 保留字: def在 Scala 定義一個方法以 def 為開始, ex: def fish ... name: typedef fish: Int 方法若不需傳遞參數, 則可省略( ) def fish = 2 public Integer fish( ) { return 2 } // Java style 可變長度引數(Varargs)方法可變長 argument 被視為 Seq[_] scala> def fish(a: Int*) = a.foreach( print ) scala> def fish(a: Int*) = a.foreach( print _ ) scala> def fish(a: Int*) = a.foreach( print(_) ) fish: (a: Int*)Unit scala> val list = 1::2::3::4::5::Nil list: List[Int] = List(1, 2, 3, 4, 5) scala> fish(list: _*) 12345 深入參考:http://daily-scala.blogspot.com/search/label/varargs 預設參數與指參[v2.8]default parameter [2.8] def sum(n:Int, total:Int=0):Int = if(n==0) total else sum(n-1, n+total) named parameter [2.8] def foo(widht:Int, height:Int) = { ... } ; foo(height=100, width=200) scala> def fish(a: Int = 10, b: Int) { print (a + b) } fish: (a: Int,b: Int)Unit scala> fish(b = 10) 20 arguments:The things you specify between the parentheses when you’re invoking a method: doStuff(“a”, 2); // invoking doStuff, so a & 2 are arguments parameters: The things in the method’s signature that indicate what the method must receive when it’s invoked: void doStuff(String s, int a) { } // we’re expecting two // parameters: String and int http://blog.csdn.net/netHibernate/archive/2010/08/11/5805461.aspx 不回傳值方法(Unit)等價於 Java 對 void 的宣告,
Ex. scala> def fish( ) = println("Fish, Kuo") fish: ( )Unit 空白的回應括號說明函數不帶參數。Unit 是 fish 的結果類型。Unit 的結果類型指的是函數沒有返回有用的值。Scala的Unit類型比較接近 Java 的 void 類型,而且實際上 Java 裡每一個返回 void 的方法都被映射為 Scala 裡返回 Unit 的方法。因此結果類型為 Unit 的方法,僅僅是為了它們的副作用而運行。 scala> def fish = println("Fish, Kuo") fish: Unit 延伸閱讀, 0604.scala.Unit
回傳值方法與類型推演Scala 對方法也會嘗試推斷返回值的類型, 但
Ex. scala> def mac(x: Int, y: Int) = { | if (x > y) x | else | y | } mac: (x: Int,y: Int)Int scala> mac(8, 5) res2: Int = 8 scala> def mac(x: Int, y: Int): Int = { | if (x > y) x | else | y | } mac: (x: Int,y: Int)Int scala> mac(8, 5) res4: Int = 8 scala> def mac(x: Int, y: Int) { | if (x > y) x | else | y | } mac: (x: Int,y: Int)Unit scala> mac(8, 5) scala> def mac(x: Int, y: Int):Int = if(x > y) x else y mac: (x: Int,y: Int)Int scala> mac(8, 5) res5: Int = 8 scala> def method1() { 6 } method1: ()Unit scala> def method2() = { 6 } method2: ()Int scala> def method3() = 6 method3: ()Int scala> def method4 : Double = 6 method4: Double scala> def fish(x: Int) { 6 } fish: (x: Int)Unit scala> def fish(x: Int) = 6 fish: (x: Int)Int scala> def fish(x: Int) = { 6 } fish: (x: Int)Int scala> def fish(x: Int): Double = { 6 } fish: (x: Int)Double 方法的調用呼叫當調用方法如果僅有0..1個參數, 則 . 與 ( ) 是可以被忽略的, 來消減程式碼中的混亂.
scala> (1 to 10) foreach(i => print(i)) 12345678910 Now that you've worked with Scala variables, you'll probably want to write some methods. Here's how you do that in Scala:
Method definitions start with
Sometimes the Scala compiler will require you to specify the result type of a method. If the method is recursive1,
for example, you must explicitly specify the method result type. In the case of scala> def max2(x: Int, y: Int) = if (x < y) y else x Note that you must always explicitly specify a method's parameter types regardless of whether you explicitly specify its result type.
The name, parameters list, and result type, if specified, form a method's signature.
After the method's
signature you must put an equals sign and then the body of the method. Since scala> def max3(x: Int, y: Int) = { if (x < y) y else x } If you want to put more than one statement in the body of a method, you must enclose them inside curly braces. Once you have defined a method, you can call it by name, as in: scala> max(3, 5) Note that if a method takes no parameters, as in: scala> def greet() = println("Hello, world!") You can call it with or without parentheses: scala> greet()
The recommended style guideline for such method invocations is that if the method may have side effects4, you should
provide the parentheses even if the compiler doesn't require them. Thus in this case, since the |
C05.入門與語法(Syntax) >