gpt4 book ai didi

kotlin - 我可以在 Kotlin 中使用 is null 而不是 isInitialized 吗?

转载 作者:行者123 更新时间:2023-12-02 07:57:32 26 4
gpt4 key购买 nike

以下代码A来自项目play-billing-samples .

我可以在 Kotlin 中使用 is 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)
}
...
}

关于kotlin - 我可以在 Kotlin 中使用 is null 而不是 isInitialized 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61810531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com