scala 方法与函数

scala 方法与函数

定义方法

定义方法的基本格式是:
def 方法名称 (参数列表):返回值类型 = 方法体,Scala中 方法规则是如果没有入参则尽量只用方法名定义该方法。

def add(x:Int,y:Int):Int = x+y
//也可以定义成
//def add(x:Int,y:Int)=x+y
//或者
def add(x:Int,y:Int){x+y}
//没有返回值一定要用大括号那方法体括起来

带有参数列表的方法

def addThenMultiply(x:INt,y:Int)(multipier:Int):Int=(x+y)*multipier 
println(addThenMultiply(1,2)(3))

无参数方法

scala> def name:String = System.getProperty("user.name")
name: String

scala> name
res2: String = apple

scala> name()
<console>:9: error: not enough arguments for method apply: (index: Int)Char in class StringOps.
Unspecified value parameter index.
              name()
                  ^

scala> def name():String = System.getProperty("user.name")
name: ()String

scala> name()
res4: String = apple

scala> name
res5: String = apple

方法体是多行语句的表达式

def getSquareString(input:Double):String = {
	val square =input*input
	square.toString
}

带有默认值的方法

默认参数,可以有多个。对位置不作要求

def method1(a:Int=1,b:Int,c:Int=3)=println("a="+a+"b= "+b+"c="+c)
//调用
method1(b=2) //不能method1(2)
//或者
method1(1,2)//method1(a=1,b=2)
//或则
method1(1,2,3) //method1(c=1,b=2,a=3)

可变参数或者不定长参数

注意1:(可变参数一定是参数列表的最后一个参数)
注意2:(函数内部,重复参数类型是声明参数类型的数组,但是如果给你一个可变参数的方法传一个数组,是会报编译错误 :type mismatch ,可以使用数组名:_*的方式传参)

def add(a:Int*)
{ 
   for(i<-a) 
     println(i); 
}

调用:
add(1,2,3,4,5) 
或者: 
var arr = Array(1,2,3,4,5,8) 
add(arr:_*)

注意:

  1. 方法的返回值类型不可不写,编译器可以自动推断出来,但是对于递归函数,必须制定返回类型
  2. 方法的返回值默认是方法体中最后一行表达式的值,方然也可以用return来执行返回值,但不推荐这么做
  3. 若使用return来制定函数的返回值。scala的类型推断将会失效,要显式制定返回值类型
  4. 方法也可以没有返回值(返回值是Unit)
     

定义函数

给方法传递一个函数类型的参数(高级静态语言):

def foo(f:Int => String) =...
def bar(f:(Boolean,Double)=>List[String]) = ...

函数定义的方式:

val f1 = ((a:Int,b:Int) =>a+b)
val f2 = (a:Int,b:Int) =>a+b
val f3 = (_:Int)+(_:Int)
val f4 :(Int,Int)=>Int = (_+_)
val f1 = (x:Int,y:Int)=>x+y

//调用函数
f1(1,2)

的定义方式:

val f1:(Int,Int)=>Int=(x,y)=>x+y
val f2:((Int,Int)=>Int)={(x,y)=>x+y}

val f1 = new Function2[Int,Int,Int]{
def apply(x:Int,y:Int):Int = if(x<y)y else x
}

匿名函数

(x:Int)=>x+1

无参函数

val getTheAnswer =()=>42
println(getTheAnswer())

方法和函数的区别
方法和函数的定义的语法不同
方法一般定义在某个类,特质,或者object中
方法可以共享所在类内的属性
在函数式编程语言中,函数式”头等公民”,可以调用它,也可以传递它,存放在变量中,或者作为参数传递给另一个函数

案例:首先定义一个方法,然后将函数传递到方法
 

scala> def m(f:(Int,Int)=>Int) = f(2,6)
m: (f: (Int, Int) => Int)Int

scala> val f2 = (x:Int,y:Int)=>x-y
f2: (Int, Int) => Int = <function2>

m(f2)
res17: Int = -4


scala> def m(f:(Int,Int)=>Int) = f(2,6)
m: (f: (Int, Int) => Int)Int

scala> def f2(x:Int,y:Int)= x-y
f2: (x: Int, y: Int)Int

scala> m(f2)
res3: Int = -4

scala>

方法转换成函数

把方法作为参数传给一个方法或者函数的时候。方法被转换为函数
使用神奇的下划线 _

scala> def m1(x:Int,y:Int) = x+y
m1: (x: Int, y: Int)Int

scala> m1 _
res19: (Int, Int) => Int = <function2>

scala> m1(_,_)
res20: (Int, Int) => Int = <function2>

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.

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 = <function1>表示function f1的method体接受一个Int型的参数,输出结果的类型是Int型。

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

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

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

关注公众号 海量干货等你
原文地址:https://www.cnblogs.com/sowhat1412/p/12734191.html