一
1.1
可以使用 call by name notation: 忽略 (),
object A
{
def runInThread(block: => Unit)
{
new Thread {
override def run() { block }
}.start()
}
//这个是中规中矩的写法,调用的时候语法不整洁
def runInThread2(block: ()=> Unit)
{
new Thread {
//************注意调用时候有括号
override def run() { block() }
}.start()
}
def main (args:Array[String]): Unit =
{
runInThread { println("Hi"); Thread.sleep(10000); println("Bye") }
runInThread2 { () => println("Hi"); Thread.sleep(10000); println("Bye") }
}
}
网友评论