这是斯卡拉代码:
class FullDescription(val description: String)
class Logger {
val description = //?????
def onAttach(description: String) =
description = new FullDescription(description)
// val lazy description = onAttach(???) --
// ops! we don't know its actual argument,
// besides we should be able to make
// val description be evaluated from onAttach(...) and not vise versa
}
假设对于任何类Logger
,都保证该方法onAttach
只会被调用一次。但不知道何时onAttach
调用该方法。
是否有可能保持val description
不变,我该怎么做?评价lazy
还是别的?