什么是闭包
闭包是一个函数,函数的返回值依赖于函数外部的一个或者多个变量
object FunDemo1 extends App {
var factor = 5
val mul = (x: Int) => x * 5
val mul2 = (x: Int) => x * factor
println(mul2(10))
factor = 10
println(mul2(10))
val mul3 = (x: Int) => {
factor = factor + 10
x * factor
}
println("factor=" + factor)
mul3(10)
println("factor=" + factor)
}