导读 Scala 有方法与函数,二者在语义上的区别很小。Scala 方法是类的一部分,而函数是一个对象可以赋值给一个变量。换句话来说在类中定义的函数即是方法。

如何定义

首先,还是引用英文原文来看看他们在定义上的区别和联系吧:

A Function Type is (roughly) a type of the form (T1, ..., Tn) => U, which is a shorthand for the trait FunctionN in the standard library. Anonymous Functions and Method Values have function types, and function types can be used as part of value, variable and function declarations and definitions. In fact, it can be part of a method type.

A Method Type is a non-value type. That means there is no value - no object, no instance - with a method type. As mentioned above, a Method Value actually has a Function Type. A method type is a def declaration - everything about a def except its body.

例子:

scala> def m1(x:Int) = x+3
m1: (x: Int)Int    

scala> val f1 = (x: Int) => x+3
f1: Int => Int = <function1>

看到没,方法定义和函数定义是不是在scala的解析器signature上就有显示了,def m1(x: Int) = x+3就是一个简单的method的定义。signature中m1: (x: Int)Int 表示method m1有一个参数Int型参数x,返回值是Int型。

val f1 = (x: Int) => x+3则是function的定义,解析器的signature中f1: Int => Int = 表示function f1的method体接受一个Int型的参数,输出结果的类型是Int型。

从上面的例子,得出一个总结:

方法是一个以def开头的带有参数列表(可以无参数列表)的一个逻辑操作块,这正如object或者class中的成员方法一样。

函数是一个赋值给一个变量(或者常量)的匿名方法(带或者不带参数列表),并且通过=>转换符号跟上逻辑代码块的一个表达式。=>转换符号后面的逻辑代码块的写法与method的body部分相同。

其他区别

method 可以作为一个表达式的一部分出现(调用函数并传参),但是 method(带参方法)不能作为最终的表达式(无参方法可以,但是这个就成了方法调用,因为 scala 允许无参方法调用时省略()括号),而 function 可以作为最终的表达式出现。

scala> m1
<console>:12: error: missing arguments for method m1;
follow this method with `_' if you want to treat it as a partially applied function
       m1
       ^

scala> f1
res1: Int => Int = <function1>

method 可以没有参数列表,参数列表也可以为空。但是 function 必须有参数列表(也可以为空)。方法名意味着方法调用,函数名只是代表函数自身:

scala> def m2 = 100;
m2: Int

scala> def m3() = 1000;
m3: ()Int

scala> var f2 = => 100;
<console>:1: error: illegal start of simple expression
var f2 = => 100;
         ^

scala> var f2 =()=> 100;
f2: () => Int = <function0>

scala> m2
res2: Int = 100

scala> m3
res3: Int = 1000

scala> m3()
res4: Int = 1000

scala> f2
res5: () => Int = <function0>

scala> f2()
res6: Int = 100

在函数出现的地方我们可以提供一个方法。

这是因为,如果期望出现函数的地方我们提供了一个方法的话,该方法就会自动被转换成函数。该行为被称为 ETA expansion。

注意:

期望出现函数的地方,我们可以使用方法。

不期望出现函数的地方,方法并不会自动转换成函数。

在 scala 中操作符被解释称方法:

  • 前缀操作符:op obj 被解释称 obj.op
  • 中缀操作符:obj1 op obj2 被解释称 obj1.op(obj2)
  • 后缀操作符:obj op 被解释称 obj.op
scala> val ml = List(1,2,3,4)
ml: List[Int] = List(1, 2, 3, 4)

scala> ml.map((x)=>2*x)
res0: List[Int] = List(2, 4, 6, 8)

scala> def m(x:Int) = 2*x
m: (x: Int)Int

scala> ml.map(m)
res1: List[Int] = List(2, 4, 6, 8)

scala> def m(x:Int) = 3*x
m: (x: Int)Int

scala> ml.map(m)
res2: List[Int] = List(3, 6, 9, 12)

可以在方法名后面加一个下划线强制变成函数。

注意: 方法名与下划线之间至少有一个空格哟!

scala> def m3(x: Int): Int = x * x * x
m3: (x: Int)Int

scala> val f3 = m3_
<console>:10: error: not found: value m3_
       val f3 = m3_
                ^

scala> val f3 = m3 _
f3: Int => Int = <function1>

scala> f3(3)
res0: Int = 27

原文来自:https://www.runoob.com/w3cnote/scala-method-functio-different.html

本文地址:https://www.linuxprobe.com/scala-method-functio.html编辑:王艳敏,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/