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 //每次使用的时候都重新取值
原文地址:https://www.cnblogs.com/suanec/p/5716168.html