以下代码A来自项目play-billing-samples。
我可以在Kotlin中使用null代替isInitialized
吗?就像代码B
代码A
lateinit private var localCacheBillingClient: LocalBillingDb
val inappSkuDetailsListLiveData: LiveData<List<AugmentedSkuDetails>> by lazy {
if (::localCacheBillingClient.isInitialized == false) {
localCacheBillingClient = LocalBillingDb.getInstance(application)
}
...
}
代码B
lateinit private var localCacheBillingClient: LocalBillingDb
val inappSkuDetailsListLiveData: LiveData<List<AugmentedSkuDetails>> by lazy {
if (localCacheBillingClient == null) {
localCacheBillingClient = LocalBillingDb.getInstance(application)
}
...
}
最佳答案
是的,可以,但是您必须将变量的类型更改为LocalBillingDb?
并摆脱lateinit
修饰符:
private var localCacheBillingClient: LocalBillingDb? = null
val inappSkuDetailsListLiveData: LiveData<List<AugmentedSkuDetails>> by lazy {
if (localCacheBillingClient == null) {
localCacheBillingClient = LocalBillingDb.getInstance(application)
}
...
}