Kotlin 为什么需要阶级偏好<;T>;被物体包裹?

Kotlin 为什么需要阶级偏好<;T>;被物体包裹?,kotlin,Kotlin,以下代码A来自 当我使用代码A时,我可以使用private-var-zipCode:Long by DelegatesExt.preference(这个,ZIP代码,默认值为ZIP)来调用 我不明白作者为什么用对象DelegatesExt包装类首选项(…) 我认为代码B更简单,我可以使用private-val-zipCode:Long-by-Preference(这是邮政编码,默认邮政编码)在使用代码B时调用 为什么需要用对象包装类首选项 代码A object DelegatesExt {

以下代码A来自

当我使用代码A时,我可以使用
private-var-zipCode:Long by DelegatesExt.preference(这个,ZIP代码,默认值为ZIP)
来调用

我不明白作者为什么用对象DelegatesExt包装类首选项(…)

我认为代码B更简单,我可以使用private-val-zipCode:Long-by-Preference(这是邮政编码,默认邮政编码)在使用代码B时调用

为什么需要用对象包装类首选项

代码A

object DelegatesExt {
    fun <T> notNullSingleValue() = NotNullSingleValueVar<T>()
    fun <T> preference(context: Context, name: String,
            default: T) = Preference(context, name, default)
}

class NotNullSingleValueVar<T> {

    private var value: T? = null

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T =
            value ?: throw IllegalStateException("${property.name} not initialized")

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        this.value = if (this.value == null) value
        else throw IllegalStateException("${property.name} already initialized")
    }
}

class Preference<T>(private val context: Context, private val name: String,
        private val default: T) {

    private val prefs: SharedPreferences by lazy {
        context.getSharedPreferences("default", Context.MODE_PRIVATE)
    }

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T = findPreference(name, default)

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        putPreference(name, value)
    }

    @Suppress("UNCHECKED_CAST")
    private fun findPreference(name: String, default: T): T = with(prefs) {
        val res: Any = when (default) {
            is Long -> getLong(name, default)
            is String -> getString(name, default)
            is Int -> getInt(name, default)
            is Boolean -> getBoolean(name, default)
            is Float -> getFloat(name, default)
            else -> throw IllegalArgumentException("This type can be saved into Preferences")
        }

        res as T
    }

    @SuppressLint("CommitPrefEdits")
    private fun putPreference(name: String, value: T) = with(prefs.edit()) {
        when (value) {
            is Long -> putLong(name, value)
            is String -> putString(name, value)
            is Int -> putInt(name, value)
            is Boolean -> putBoolean(name, value)
            is Float -> putFloat(name, value)
            else -> throw IllegalArgumentException("This type can't be saved into Preferences")
        }.apply()
    }
}
objectdelegatesExt{
fun notNullSingleValue()=NotNullSingleValueVar()
趣味首选项(上下文:上下文,名称:字符串,
默认值:T)=首选项(上下文、名称、默认值)
}
类NotNullSingleValueVar{
私有变量值:T?=null
运算符fun getValue(thisRef:Any?,属性:KProperty):T=
值?:抛出IllegalStateException(${property.name}未初始化)
运算符fun setValue(thisRef:Any?,属性:KProperty,值:T){
this.value=if(this.value==null)值
else抛出IllegalStateException(${property.name}已初始化)
}
}
类首选项(私有val上下文:上下文,私有val名称:字符串,
私有值(默认值:T){
private val prefs:lazy的SharedReferences{
context.getSharedReferences(“默认”,context.MODE\u PRIVATE)
}
运算符fun getValue(thisRef:Any?,属性:KProperty):T=findReference(名称,默认值)
运算符fun setValue(thisRef:Any?,属性:KProperty,值:T){
putPreference(名称、值)
}
@抑制(“未选中的_CAST”)
private fun FindReference(名称:String,默认值:T):T=with(prefs){
val res:Any=when(默认值){
is Long->getLong(名称,默认值)
is String->getString(名称,默认值)
是Int->getInt(名称,默认值)
is Boolean->getBoolean(名称,默认值)
是Float->getFloat(名称,默认值)
else->抛出IllegalArgumentException(“此类型可以保存到首选项中”)
}
res as T
}
@SuppressLint(“提交引用”)
private参数设置(名称:String,值:T)=带(prefs.edit()){
何时(值){
is Long->putLong(名称、值)
is String->putString(名称、值)
is Int->putInt(名称、值)
is Boolean->putBoolean(名称、值)
is Float->PUTFOAT(名称、值)
else->抛出IllegalArgumentException(“此类型无法保存到首选项中”)
}.apply()
}
}
代码B

class NotNullSingleValueVar<T> {

    private var value: T? = null

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T =
            value ?: throw IllegalStateException("${property.name} not initialized")

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        this.value = if (this.value == null) value
        else throw IllegalStateException("${property.name} already initialized")
    }
}

class Preference<T>(private val context: Context, private val name: String,
        private val default: T) {

    private val prefs: SharedPreferences by lazy {
        context.getSharedPreferences("default", Context.MODE_PRIVATE)
    }

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T = findPreference(name, default)

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        putPreference(name, value)
    }

    @Suppress("UNCHECKED_CAST")
    private fun findPreference(name: String, default: T): T = with(prefs) {
        val res: Any = when (default) {
            is Long -> getLong(name, default)
            is String -> getString(name, default)
            is Int -> getInt(name, default)
            is Boolean -> getBoolean(name, default)
            is Float -> getFloat(name, default)
            else -> throw IllegalArgumentException("This type can be saved into Preferences")
        }

        res as T
    }

    @SuppressLint("CommitPrefEdits")
    private fun putPreference(name: String, value: T) = with(prefs.edit()) {
        when (value) {
            is Long -> putLong(name, value)
            is String -> putString(name, value)
            is Int -> putInt(name, value)
            is Boolean -> putBoolean(name, value)
            is Float -> putFloat(name, value)
            else -> throw IllegalArgumentException("This type can't be saved into Preferences")
        }.apply()
    }
}
类NotNullSingleValueVar{
私有变量值:T?=null
运算符fun getValue(thisRef:Any?,属性:KProperty):T=
值?:抛出IllegalStateException(${property.name}未初始化)
运算符fun setValue(thisRef:Any?,属性:KProperty,值:T){
this.value=if(this.value==null)值
else抛出IllegalStateException(${property.name}已初始化)
}
}
类首选项(私有val上下文:上下文,私有val名称:字符串,
私有值(默认值:T){
private val prefs:lazy的SharedReferences{
context.getSharedReferences(“默认”,context.MODE\u PRIVATE)
}
运算符fun getValue(thisRef:Any?,属性:KProperty):T=findReference(名称,默认值)
运算符fun setValue(thisRef:Any?,属性:KProperty,值:T){
putPreference(名称、值)
}
@抑制(“未选中的_CAST”)
private fun FindReference(名称:String,默认值:T):T=with(prefs){
val res:Any=when(默认值){
is Long->getLong(名称,默认值)
is String->getString(名称,默认值)
是Int->getInt(名称,默认值)
is Boolean->getBoolean(名称,默认值)
是Float->getFloat(名称,默认值)
else->抛出IllegalArgumentException(“此类型可以保存到首选项中”)
}
res as T
}
@SuppressLint(“提交引用”)
private参数设置(名称:String,值:T)=带(prefs.edit()){
何时(值){
is Long->putLong(名称、值)
is String->putString(名称、值)
is Int->putInt(名称、值)
is Boolean->putBoolean(名称、值)
is Float->PUTFOAT(名称、值)
else->抛出IllegalArgumentException(“此类型无法保存到首选项中”)
}.apply()
}
}
当我使用代码A时,我可以使用private-var-zipCode:Long by DelegatesExt.preference(这是ZIP\u代码,默认ZIP)来调用

我认为代码B更简单,当我使用代码B时,我可以使用private val zipCode:Long-by-Preference(这是ZIP_-Code,DEFAULT_-ZIP)来调用

在第一种情况下,您还可以导入
DelegatesExt.*
DelegatesExt.preference
而不是
DelegatesExt
并按首选项写入

为什么需要用对象包装类首选项

不需要(我也不会这么做),这只是作者的偏好

当我使用代码A时,我可以使用private-var-zipCode:Long by DelegatesExt.preference(这是ZIP\u代码,默认ZIP)来调用

我认为代码B更简单,当我使用代码B时,我可以使用private val zipCode:Long-by-Preference(这是ZIP_-Code,DEFAULT_-ZIP)来调用

在第一种情况下,您还可以导入
DelegatesExt.*
DelegatesExt.preference
而不是
DelegatesExt
并按首选项写入

为什么需要用对象包装类首选项

不需要(我也不会这么做),这只是作者的偏好