这是斯卡拉代码:
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还是别的?