Kotlin中的私有构造函数用于什么?
我是科特林的新手。我想问一下Kotlin中的私有构造函数用于什么<代码>类DontCreateMe私有构造函数(){/*…*/}。我的意思是,如果我们不能创建它的实例,那么类应该是什么呢?好吧,注释中的答案是正确的,但是因为没有人写完整的答案。我要试一试 拥有私有构造函数并不一定意味着外部代码不能使用对象。这只是意味着外部代码不能直接使用其构造函数,因此它必须通过类范围中的公开API获取实例。由于此API位于类范围内,因此它可以访问私有构造函数 最简单的例子是:Kotlin中的私有构造函数用于什么?,kotlin,constructor,Kotlin,Constructor,我是科特林的新手。我想问一下Kotlin中的私有构造函数用于什么类DontCreateMe私有构造函数(){/*…*/}。我的意思是,如果我们不能创建它的实例,那么类应该是什么呢?好吧,注释中的答案是正确的,但是因为没有人写完整的答案。我要试一试 拥有私有构造函数并不一定意味着外部代码不能使用对象。这只是意味着外部代码不能直接使用其构造函数,因此它必须通过类范围中的公开API获取实例。由于此API位于类范围内,因此它可以访问私有构造函数 最简单的例子是: class ShyPerson priv
class ShyPerson private constructor() {
companion object {
fun goToParty() : ShyPerson {
return ShyPerson()
}
}
}
fun main(args: String) {
// outside code is not directly using the constructor
val person = ShyPerson.goToParty()
// Just so you can see that you have an instance allocated in memory
println(person)
}
class Unity private constructor() {
companion object {
private var INSTANCE : Unity? = null
// Note that the reason why I've returned nullable type here is
// because kotlin cannot smart-cast to a non-null type when dealing
// with mutable values (var), because it could have been set to null
// by another thread.
fun instance() : Unity? {
if (INSTANCE == null) {
INSTANCE = Unity()
}
return INSTANCE
}
}
}
fun main(args: Array<String>) {
val instance = Unity.instance()
println(instance)
}
我看到的最常见的用例是实现Singleton模式,如Mojtaba Haddadi所述,其中外部代码只能访问类的一个实例
一个简单的实施方案是:
class ShyPerson private constructor() {
companion object {
fun goToParty() : ShyPerson {
return ShyPerson()
}
}
}
fun main(args: String) {
// outside code is not directly using the constructor
val person = ShyPerson.goToParty()
// Just so you can see that you have an instance allocated in memory
println(person)
}
class Unity private constructor() {
companion object {
private var INSTANCE : Unity? = null
// Note that the reason why I've returned nullable type here is
// because kotlin cannot smart-cast to a non-null type when dealing
// with mutable values (var), because it could have been set to null
// by another thread.
fun instance() : Unity? {
if (INSTANCE == null) {
INSTANCE = Unity()
}
return INSTANCE
}
}
}
fun main(args: Array<String>) {
val instance = Unity.instance()
println(instance)
}
class Unity私有构造函数(){
伴星{
私有变量实例:Unity?=null
//请注意,我在这里返回nullable类型的原因是
//因为kotlin在处理时不能智能转换为非null类型
//使用可变值(var),因为它可能被设置为null
//另一条线索。
有趣的例子():统一{
if(实例==null){
INSTANCE=Unity()
}
返回实例
}
}
}
趣味主线(args:Array){
val instance=Unity.instance()
println(实例)
}
这通常用于使消耗资源的类只实例化一次,或使某些数据片段被整个代码库共享
请注意,kotlin使用实现此模式,具有线程安全的优点。也有一些开发商认为
私有构造函数的另一个用例是实现,其中具有复杂初始化的类可以抽象为更简单的API,因此用户不必处理笨拙的构造函数。这说明了它在kotlin中的用途
我在现实生活中看到的kotlin代码中最简单的用法之一是在stdlib中的,它被用来更改对象的内部表示。如果类仅包含静态方法(即kotlin中有一个伴生对象),则私有构造函数非常有用,或者,如果它只应该由类本身或其伴生对象中定义的方法实例化。例如,请参见java.util.Optional类。您可以创建它的实例。但不是通过调用它的构造函数。仅使用工厂方法,如of()或ofNullable(),或使用map()将可选的转换为另一个。签出此链接:,代码是java的,但概念是相同的。如果您有一个私有主构造函数,但有多个公共辅助构造函数,则可以限制参数的某些组合。“如果类只包含静态方法(即在Kotlin中有一个伴生对象)”,在这种情况下,它应该是
对象
,而不是一个带有私有构造函数和伴生对象中所有方法的类。@AlexeyRomanov同意。这更像是对JVM上一般的私有构造函数的解释。