class ParametersPartialFunction (f: (Int) => PartialFunction[Int,Int]) {
def m1(site:Int):String = {
val g = f(site) //并不是常规意义上的调用函数来获取比如Int值,而是调用特殊函数,只不过这个函数输入一个Int,返回的是一个偏函数
if(g.isDefinedAt(site)) g(site).toString //到了这一行,则是对偏函数进行isDefinedAt和apply调用了
else "null value"
}
}
object ParametersPartialFunction {
def main(args: Array[String]): Unit = {
val p = new ParametersPartialFunction( x => {
case x if (x == 1 || x == 2) => x + 1
})
val v = p.m1(1)
val v2 = p.m1(3)
println(v)
println(v2)