周末班补充视频Scala第2课:动手编写和运行自己的第一个Scala函数式编程的实例.
package com.dtspark.scala.functional.basics
object MyFirstFunctionalAPP { def add(x:Int,y:Int): Int = { x+y } def sub(x:Int,y:Int):Int ={ x-y } def formatResult(x:Int,y:Int,f:(Int,Int)=>Int) :String= { println("the result test : " + f(x,y)) "the result : %d ".format(f(x,y)) } def main (args:Array[String]):Unit = { println(formatResult(1,2,add)) println(formatResult(1,2,sub)) } }
测试 结果
the result test : 3 the result : 3 the result test : -1 the result : -1