我有一個課,我要麼知道創造的具體價值,要麼我需要生成它,這有點貴。只有在實際需要時纔可以生成該值?我可以根據構造函數初始化一個值嗎?
val expensiveProperty: A
constructor(expensiveProperty: A) {
this.expensiveProperty = expensiveProperty
}
constructor(value: B) {
// this doesn't work
this.expensiveProperty = lazy { calculateExpensiveProperty(value) }
}
[this](https://stackoverflow.com/a/36233649/6521116)可能有幫助 –