1.不帶參數(shù)
//lazy實現(xiàn)
class Single private constructor(){
private object Holder {
val INSTANCE = Single()
}
companion object {
val instance :Single by lazy { Holder.INSTANCE }
}
}
//
class Single private constructor() {
companion object {
val instance = Single()
}
}
2.帶參數(shù)
class Single private constructor(context: Context) {
companion object {
var instance: Single? = null
fun getInstance(context: Context): Single {
if (instance == null) {
synchronized(Single::class) {
if (instance == null) {
instance = Single(context)
}
}
}
return instance!!
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者