scala学习笔记:理解lazy值

scala> var counter = 0
counter: Int = 0

scala> def foo = {counter += 1; counter}
foo: Int

scala> val value1 = foo; lazy val value2 = foo; def value3 = foo
value1: Int = 1
value2: Int = <lazy>
value3: Int

scala> value1
res39: Int = 1

scala> value1
res40: Int = 1

scala> value2
res41: Int = 2

scala> value2
res42: Int = 2

scala> value3
res43: Int = 3

scala> value3
res44: Int = 4

你可能感兴趣的:(scala学习笔记:理解lazy值)