构造函数可见性仅限于文件
Constructor visibility restricted to file
我想创建一种更简单的方法来处理 SharedPreferences。
我想这样称呼它
获得偏好:
val email = SharedPrefs.userdata.email
val wifiOnly = SharedPrefs.connections.wifiOnly
设置首选项:
SharedPrefs.userdata.email = "someone@example.com"
SharedPrefs.connections.wifiOnly = true
我可以这样做:
App.instance
returns 以下代码段中的一个 Context
对象
object SharedPrefs {
val userdata by lazy { UserPreferences() }
val connections by lazy { ConnectionPreferences() }
class UserPreferences {
private val prefs: SharedPreferences = App.instance.getSharedPreferences("userdata", Context.MODE_PRIVATE)
var email: String
get() = prefs.getString("email", null)
set(value) = prefs.edit().putString("email", value).apply()
}
class ConnectionPreferences {
private val prefs: SharedPreferences = App.instance.getSharedPreferences("connections", Context.MODE_PRIVATE)
var wifyOnly: Boolean
get() = prefs.getBoolean("wifiOnly", false)
set(value) = prefs.edit().putBoolean("wifyOnly", value).apply()
}
}
问题是这仍然可以调用:SharedPrefs.UserPreferences()
我可以让这个构造函数只对这个文件或对象私有吗?
你可以试试这个
class UserPreferences private constructor()
{
// your impl
}
This为参考
你可以把接口和实现分开class,让后者private
成为对象:
object SharedPrefs {
val userdata: UserPreferences by lazy { UserPreferencesImpl() }
interface UserPreferences {
var email: String
}
private class UserPreferencesImpl : UserPreferences {
private val prefs: SharedPreferences =
App.instance.getSharedPreferences("userdata", Context.MODE_PRIVATE)
override var email: String
get() = prefs.getString("email", null)
set(value) = prefs.edit().putString("email", value).apply()
}
// ...
}
或者,如果您正在开发一个库或者您有一个模块化架构,您可以使用 internal
visibility 修饰符来限制模块的可见性:
class UserPreferences internal constructor() { /* ... */ }
我想创建一种更简单的方法来处理 SharedPreferences。 我想这样称呼它
获得偏好:
val email = SharedPrefs.userdata.email
val wifiOnly = SharedPrefs.connections.wifiOnly
设置首选项:
SharedPrefs.userdata.email = "someone@example.com"
SharedPrefs.connections.wifiOnly = true
我可以这样做:
App.instance
returns 以下代码段中的一个 Context
对象
object SharedPrefs {
val userdata by lazy { UserPreferences() }
val connections by lazy { ConnectionPreferences() }
class UserPreferences {
private val prefs: SharedPreferences = App.instance.getSharedPreferences("userdata", Context.MODE_PRIVATE)
var email: String
get() = prefs.getString("email", null)
set(value) = prefs.edit().putString("email", value).apply()
}
class ConnectionPreferences {
private val prefs: SharedPreferences = App.instance.getSharedPreferences("connections", Context.MODE_PRIVATE)
var wifyOnly: Boolean
get() = prefs.getBoolean("wifiOnly", false)
set(value) = prefs.edit().putBoolean("wifyOnly", value).apply()
}
}
问题是这仍然可以调用:SharedPrefs.UserPreferences()
我可以让这个构造函数只对这个文件或对象私有吗?
你可以试试这个
class UserPreferences private constructor()
{
// your impl
}
This为参考
你可以把接口和实现分开class,让后者private
成为对象:
object SharedPrefs {
val userdata: UserPreferences by lazy { UserPreferencesImpl() }
interface UserPreferences {
var email: String
}
private class UserPreferencesImpl : UserPreferences {
private val prefs: SharedPreferences =
App.instance.getSharedPreferences("userdata", Context.MODE_PRIVATE)
override var email: String
get() = prefs.getString("email", null)
set(value) = prefs.edit().putString("email", value).apply()
}
// ...
}
或者,如果您正在开发一个库或者您有一个模块化架构,您可以使用 internal
visibility 修饰符来限制模块的可见性:
class UserPreferences internal constructor() { /* ... */ }