scala函数和方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013803572/article/details/79781731

方法

// def是声明方法的关键字,hello方法名,str:String参数和类型,:Unit返回值类型,= 用于连接方法签名和方法体
scala> def hello(str: String): Unit = {  
     |   println("hello" + str)
     | }
hello: (str: String)Unit

scala> hello("world")
helloworld
scala> def m1(a: Int, b: Int):Int = {
     |   a * b
     | }
m1: (a: Int, b: Int)Int

scala> m1(3,4)
res2: Int = 12
  • 返回类型和等于号可以省略,当没有等于号时其默认的返回类型是Unit,即无值
scala> def hello(str: String) {  // 返回类型为Unit时可以省略返回类型和等于号
     |   println("hello" + str)
     | }
hello: (str: String)Unit
scala> hello("there")
hellothere

scala> def m1(a: Int, b: Int) = {  // 不写返回类型,有等于号,scala会抬头推导其返回值的类型
     |   a * b
     | }
m1: (a: Int, b: Int)Int
scala> m1(3,9)
res5: Int = 27

scala> def m1(a: Int, b: Int) {  // 不写返回类型也不写等于号,其返回类型是Unit
     |   a * b
     | }
m1: (a: Int, b: Int)Unit
scala> m1(3, 4)

函数(如有错误敬请指正)

  • 函数是scala的一等公民,可以使用在任何地方,可以赋值给一个变量,可以作为一个方法的参数,也可以作为一个函数的参数,其实方法也可以作为一个方法的参数或者函数的参数。scala中的任何对象,方法,函数都可以作为参数。
// 方法要用def去定义,函数需要一个变量去接收或者直接作为参数。
// 函数最明显的一个特点是包含 => ,包含这个符号的就是函数。
// 方法是用等于号连接方法名和方法体的,而函数是用 => 连接参数和方法体的,
// 函数没有函数名,你可以把这个函数赋给一个变量,
// 可以通过这个变量调用这个函数,从某种意义上来说这个变量就是这个函数的名字
scala> val f1 = (x: Int, y: Int) => {
     |       x + y
     |     }
f1: (Int, Int) => Int = <function2>
scala> f1(3,7)
res6: Int = 10

// 函数体或者方法体只有一行代码时可以省略大括号
scala> val f1 = (x: Int, y: Int) => x + y 
f1: (Int, Int) => Int = <function2>
scala> f1(3,8)
res7: Int = 11

// 定义一个方法,其参数是一个函数或者方法,只要函数或方法的参数和返回值符合其形式:(Int, Int) => Int
scala> def m1(m: (Int, Int) => Int) = {  
     |   m(3, 10)
     | }
m1: (m: (Int, Int) => Int)Int

scala> m1(f1) 
res8: Int = 13

scala> val f2 = (x:Int, y:Int) => x * y
f2: (Int, Int) => Int = <function2>
scala> m1(f2)
res9: Int = 30

scala> def m1(m: (Int, Int) => Int, a:Int, b:Int) = {
     |   m(a, b)
     | }
m1: (m: (Int, Int) => Int, a: Int, b: Int)Int
scala> m1(f1, 3, 11)
res12: Int = 14
  • 函数式编程
scala> val arr = Array(1, 3, 5)
arr: Array[Int] = Array(1, 3, 5)

scala> val f1 = (x: Int) => x * 2
f1: Int => Int = <function1>

scala> arr.map(f1)
res10: Array[Int] = Array(2, 6, 10)
  • 将一个函数作为一个方法的参数
// 定义一个函数
scala> val function = (x:Int, y:Int) => {
     |   x + y
     | }
function: (Int, Int) => Int = <function2>

// 定义一个方法
scala> def method(x:Int,y:Int) = {
     |   x * y / 3
     | }
method: (x: Int, y: Int)Int

// 定义一个函数,其参数是一个方法或函数
scala> val function1 = (operator:(Int,Int) => Int) => {
     |   operator(10, 11);
     | }
function1: ((Int, Int) => Int) => Int = <function1>

// 定义一个方法,其参数是一个方法或函数
scala> def method1(operator:(Int,Int) => Int) = {
     |   operator(10, 11)
     | }
method1: (operator: (Int, Int) => Int)Int

// 调用funtion1,此时传入的参数是一个函数
scala> function1(function)
res1: Int = 21

// 调用funtion1,此时传入的参数是一个方法
scala> function1(method)
res2: Int = 36

// 调用method1,此时传入的参数是一个函数
scala> method1(function)
res3: Int = 21

// 调用method1,此时传入的参数是一个方法
scala> method1(method)
res4: Int = 36
  • 经过以上测试,scala中的函数和方法是可以通用的。我觉的scala中之所以还要区分函数和方法,是因为在OOP编程中一个类的行为要用方法去定义。

猜你喜欢

转载自blog.csdn.net/u013803572/article/details/79781731