如何做一张图片的网站,wordpress用户标签,it外包公司怎么接项目,在意派建设好网站后1. 方法
在scala中的操作符都被当成方法存在#xff0c;比如说、-、*、/
12就是1.(2)的调用#xff0c;
2.0 是doule类型#xff0c;强调用Int类型的写法为1.(2:Int)
1.1 方法的声明和使用
定义方法的语法#xff1a;
def 方法名([变量#xff1a;变量类型#xff…1. 方法
在scala中的操作符都被当成方法存在比如说、-、*、/
12就是1.(2)的调用
2.0 是doule类型强调用Int类型的写法为1.(2:Int)
1.1 方法的声明和使用
定义方法的语法
def 方法名([变量变量类型变量变量类型]):返回值类型{方法体}
其中
在scala 中方法里面的最后一个表达式的值就是方法的返回值不需要return 返回
示例
定义无参无返回值的方法
// 定义无参无返回值的方法
scala def say():Unit {println(say hello)}
say: ()Unit
scala say()
say hello
// 简化过程
scala def say():Unit {println(say hello)}
say: ()Unit
// 方法体有一个语句省略{}
scala def say():Unit println(say hello)
say: ()Unit
// 方法返回值可以由方法体返回结果类型推测
scala def say() println(say hello)
say: ()Unit
// 方法形参列表是空 可省略()
scala def say println(say hello)
say: Unit
scala say
say hello
scala say()
console:13: error: Unit does not take parameterssay()
// 带有返回值的方法
def add(a:Int, b:Int):Int{val c a b; return c}
定义带有有参有返回值方法
// 定义带有有参有返回值方法
scala def add(a:Int, b:Int):Int{val c a b; return c}
add: (a: Int, b: Int)Int
scala add(4,5)
res8: Int 9
// 简化流程
scala def add(a:Int, b:Int):Int{val c a b; return c}
add: (a: Int, b: Int)Int
// scala 不建议用return返回方法结果默认最后一个就是方法的返回值
scala def add(a:Int, b:Int):Int{val c a b; c}
add: (a: Int, b: Int)Int
// 去掉中间变量c
scala def add(a:Int, b:Int):Int{a b}
add: (a: Int, b: Int)Int
// 方法体有一个语句省略{}
scala def add(a:Int, b:Int):Inta b
add: (a: Int, b: Int)Int
// 方法返回值可以由方法体返回结果类型推测
scala def add(a:Int, b:Int)a b
add: (a: Int, b: Int)Int
scala add(4,5)
res9: Int 9
方法的调用
object M1 {def say(name:String) {println(ssay ${name})}def add(a:Int, b:Int) a bdef main(args: Array[String]): Unit {// 普通调用M1.say(hainiu)// 中缀方法调用M1 say hainiu// 大括号调用当只有一个入参时才能用M1 say {hainiu}M1.add(4,5)// 中缀方法调用M1 add (4,5)}
}
递归方法必须加返回值类型
scala def calculate(number:Int):Int {| if(number 1)| 1| else| number*calculate(number-1)| }
calculate: (number: Int)Intscala calculate(5)
res14: Int 120//文件夹递归方式查询def listFiles(path:String):Unit {val file new File(path)if(file.isFile){println(file.getPath)}else{println(file.getPath)val list file.list()for(p-list){listFiles(path/p)}}}
可变参数
scala def sum(x:Int*){| var sum 0| for(i-x)| sum i| sum| }
sum: (x: Int*)Intscala sum(1,2,3)
res15: Int 6scala sum(1,2,3,4,5)
res16: Int 15scala def sum(x:Int,y:Int*){| var sum 0| for(i-y)| sum i| x * sum| }
sum: (x: Int, y: Int*)Intscala sum(2,1,2,3)
2. 函数
在 java 中方法和函数是一个意思在 scala 中方法和函数是两种含义。
方法属于类或对象的成员
函数是对象
在 scala 中函数是一等公民。可以在任何地方定义在函数内或函数外可以作为函数的参数和返回值函数还可以赋给变量。
函数声明
val 变量名:[变量类型1,变量类型2 函数体返回类型 ] ([变量变量类型变量变量类型]) 函数体
示例
// 函数本身是没有名字的--匿名函数
// function2 是 函数有 两个输入参数 和 一个输出 本例是 两个Int输入一个Int输出
scala (a:Int, b:Int) a b
res10: (Int, Int) Int function2
scala res10(4,5)
res11: Int 9
// 把匿名函数赋给变量这个变量名称就成了函数名称
scala val add:(Int,Int)Int (a:Int, b:Int) a b
add: (Int, Int) Int function2
scala add(4,5)
res12: Int 9
函数的结果做为方法的参数
示例
// 定义周长函数
val perimeter (a:Int,b:Int) (ab) *2
// 定义面积函数
val area (a:Int, b:Int) a*b
// 定义求和方法
def add(a:Int, b:Int) ab
// 计算长方形周长和面积的和
println(add(perimeter(4,5), area(4,5)))
函数作为参数和返回值
scala add2
res22: (Int, Int) Int $Lambda$1133/80147637359919f37scala val add add2
add: (Int, Int) Int $Lambda$1133/80147637359919f37scala add(100,200)
res23: Int 300scala def calculate(x:Int,y:Int) xy
calculate: (x: Int, y: Int)Intscala def calculate(x:Int,y:Int,f:(Int,Int)Int) f(x,y)
calculate: (x: Int, y: Int, f: (Int, Int) Int)Intscala calculate(10,20,add)
res24: Int 30scala def sub(a:Int,b:Int) a-b
sub: (a: Int, b: Int)Intscala val sub (a:Int,b:Int) a-b
sub: (Int, Int) Int $Lambda$1137/1087688958784ceacbscala calculate(1,2,sub)
res25: Int -1scala def getFunc() {| val func (x:Int,y:Int) xy| func| }
getFunc: ()(Int, Int) Intscala getFunc()(1,2)
res26: Int 3scala def getFunc(a:Int,b:Int) {| val func (x:Int) a*x b| func| }
getFunc: (a: Int, b: Int)Int Intscala getFunc(2,1)(10)
res27: Int 21
匿名函数
scala def calculate(x:Int,y:Int) xy
calculate: (x: Int, y: Int)Intscala def calculate(x:Int,y:Int,f:(Int,Int)Int) f(x,y)
calculate: (x: Int, y: Int, f: (Int, Int) Int)Intscala val add (x:Int,y:Int) xy
add: (Int, Int) Int $Lambda$1044/98932130177049094scala calculate(10,20,add)
res9: Int 30scala calculate(10,20,(x:Int,y:Int) xy)
res10: Int 30scala (x:Int,y:Int) xy
res11: (Int, Int) Int $Lambda$1068/1578712821106c988scala res11(200,300)
res12: Int 500scala calculate(10,20,(x,y)xy)
res13: Int 30scala (x,y) xy
console:1: error: ; expected but found.(x,y) xy^
方法转换成函数
1用空格下划线的方式
# 定义方法def add_def(a:Int,b:Int) a b# 方法转函数用空格下划线的方式val add_func add_def空格_
2也可以把方法当参数使用这也因为scala会隐式的把方法转换成函数但并不是直接支持方法当参数的模式只是做了隐式的转换这种函数的转换分两种显示用空格_和隐式的这也体现了scala灵活的地方。
cala def add(x:Int,y:Int) xy
add: (x: Int, y: Int)Intscala add
console:13: error: missing argument list for method add
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing add _ or add(_,_) instead of add.add^
scala add _
res16: (Int, Int) Int $Lambda$1076/287628665335cdd2scala calculate(1,2,add _)
res17: Int 3scala calculate(1,2,add)
res18: Int 3