现在的位置: 首页 > 综合 > 正文

Scala method(方法) 和 functio(函数) 的区别

2020年02月14日 综合 ⁄ 共 2625字 ⁄ 字号 评论关闭

  在我没有学习 scala 的时候,主要用 java 和 python 做日常工作开发,印象中,没有特别的刻意的去区分 method 和 function 的区别,这个关系,正如我们日常生活中,没有刻意的去区分质量和重量。但是,他们之间,的确是有所不同的,这些不同也是建立在他们之间有联系的基础之上!

  method 和 function是什么

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

  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 =

  看到没,方法定义和函数定义是不是在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 和 function区别

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

  scala> m1

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

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

  scala> def m2 = 100;

  m2: Int

  scala> def m3() = 1000;

  m3: ()Int

  scala> var f2 = => 100;

  :1: error: illegal start of simple expression

  var f2 = => 100;

  ^

  scala> var f2 =()=> 100;

  f2: () => Int =

  scala> m2

  res2: Int = 100

  scala> m3

  res3: Int = 1000

  scala> m3()

  res4: Int = 1000

  scala> f2

  res5: () => Int =

  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_

  :10: error: not found: value m3_

  val f3 = m3_

  ^

  scala> val f3 = m3 _

  f3: Int => Int =

  scala> f3(3)

  res0: Int = 27

  以上就是有关Scala method(方法) 和 functio(函数) 的区别介绍,如果还想要了解更多C语言知识,请上学步园。

抱歉!评论已关闭.