在Kotlin中,是否可以使用工厂函数创建具有私有构造函数的类的实例?
My goal is to enforce the factory function to be used and to prevent instantiation via the class's constructor.
例子:
// factory function, valid
val myInstance = myClassOf()
// class instantiation, invalid
val myInstance = MyClass()
我正在try 模仿一些内置工厂函数的行为,比如intArrayOf()
.
// works
val myIntArray = intArrayOf()
// not possible as IntArray has a private constructor
val myIntArray = IntArray()