我可以懒惰初始化一个值取决于构造函数?

我有一个课程,要么知道创造的具体价值,要么我需要创造它,这有点贵。 只有在实际需要时才能生成该值?

val expensiveProperty: A constructor(expensiveProperty: A) { this.expensiveProperty = expensiveProperty } constructor(value: B) { // this doesn't work this.expensiveProperty = lazy { calculateExpensiveProperty(value) } } 

这是可能的,但有一个转折:

 class C private constructor(lazy: Lazy) { val expensiveProperty by lazy constructor(value: B) : this(lazy { calculateExpensiveProperty(value) }) constructor(expensiveProperty: A) : this(lazyOf(expensiveProperty)) } 

请注意,我如何保持主要构造函数私有,而留下次要构造函数公开。