首页 > 解决方案 > Kotlin 中的私有构造函数是做什么用的?

问题描述

我是 Kotlin 的新手。我想问一下 Kotlin 中的私有构造函数是干什么用的?class DontCreateMe private constructor () { /*...*/ }. 我的意思是如果我们不能创建它的实例应该是什么类?

标签: kotlinconstructor

解决方案


好吧,评论中的答案是正确的,但是由于没有人写出完整的答案。我要试一试。

拥有私有构造函数并不一定意味着外部代码不能使用对象。这只是意味着外部代码不能直接使用其构造函数,因此它必须通过类范围内的公开 API 获取实例。由于此 API 在类范围内,因此它可以访问私有构造函数。

最简单的例子是:

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)
}

我见过的最常见的用例是实现单例模式,如 Mojtaba Haddadi 所述,其中外部代码只能访问该类的一个实例。

一个简单的实现是:

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)
}

这通常用于使消耗资源的类仅实例化一次或以便某些数据由整个代码库共享。

请注意,kotlin 使用object 关键字来实现此模式,具有线程安全的优势。还有一些开发者认为单例是一种反模式

私有构造函数的另一个用例是实现Builder 模式,其中具有复杂初始化的类可以抽象为更简单的 API,因此用户不必处理笨重的构造函数。这个另一个答案解决了它在 kotlin 中的用途。

我在现实生活中看到的 kotlin 代码中最简单的用途之一是 stdlib 中的Result 实现,它用于更改对象的内部表示。


推荐阅读