草庐IT

CarRepository

全部标签

kotlin - 在 Kotlin 中获取单例

我一直在查看一些Google示例代码,他们似乎使用以下代码创建了一个单例:companionobject{//ForSingletoninstantiation@Volatileprivatevarinstance:CarRepository?=nullfungetInstance(carDao:CarDao)=instance?:synchronized(this){instance?:CarRepository(carDao).also{instance=it}}}所以我知道@Volatile意味着MarkstheJVMbackingfieldoftheannotatedprope

kotlin - 在 Kotlin 中获取单例

我一直在查看一些Google示例代码,他们似乎使用以下代码创建了一个单例:companionobject{//ForSingletoninstantiation@Volatileprivatevarinstance:CarRepository?=nullfungetInstance(carDao:CarDao)=instance?:synchronized(this){instance?:CarRepository(carDao).also{instance=it}}}所以我知道@Volatile意味着MarkstheJVMbackingfieldoftheannotatedprope