val, lazy, def

1 val strVal = scala.io.Source.fromFile("test.txt").mkString 
2 //在strVal被定义的时候获取值,如果test.txt不存在,直接报异常
3  
4 lazy val strLazy = scala.io.Source.fromFile("test.txt").mkString 
5 //在strLazy第一次被使用的时候取值,如果test.txt不存在,不使用strLazy是不会报异常的,第一次访问strLazy的时候报异常 
6  
7 def strDef = scala.io.Source.fromFile("test.txt").mkString //每次使用的时候都重新取值

 

posted @ 2016-07-28 20:35  苏轶然  阅读(177)  评论(0编辑  收藏  举报