在以下 Scala 代码中:
object Timer
{
def oncePerSecond(callback: () => Unit): Unit =
{
while (true)
{
callback()
Thread.sleep(1000)
}
}
def main(args: Array[String]): Unit =
{
oncePerSecond(() =>
Console.println("Time flies... oh, you get the idea."))
}
}
当作为参数传入时,匿名函数是否首先执行,然后在循环中每秒执行一次?