我想知道如何在Kotlin中创建一个Singleton类,以便我的Util类在每次执行应用程序时只实例化一次它.但是,当我将Java类转换为Kotlin时,生成了以下代码.

Is this correct?

companion object {
    private var utilProject: UtilProject? = null

    val instance: UtilProject
        get() {
            if (utilProject == null) utilProject = UtilProject()
            return utilProject!!
        }
} 

我可以找到一个相关的100,但它有参数,我不能让它在没有参数的情况下转换.

推荐答案

Just

companion object {
    val instance = UtilProject()
} 

will do the job because the companion object itself is a language-level singleton.
(The instance will be created when the companion object is first called.)

--更新--

If you need to control when the singleton object is initialized, you can create one object for each class.

class UtilProject {
    ....
    companion object {
        val instance = UtilProject()
    }
}

class AnotherClass {
    ...
    companion object {
        val instance = AnotherClass()
        const val abc = "ABC"
    }
}

fun main(args: Array<String>) {
    val a = UtilProject.instance // UtilProject.instance will be initialized here.
    val b = AnotherClass.abc // AnotherClass.instance will be initialized here because AnotherClass's companion object is instantiated.
    val c = AnotherClass.instance
}

在这里,AnotherClass.instance是在实际调用AnotherClass.instance之前初始化的.当AnotherClass的伴生对象被调用时,它被初始化.

class UtilProject {
    ....
    companion object {
        fun f() = ...
    }
}

class AnotherClass {
    ...
    companion object {
        const val abc = "ABC"
    }
}

object UtilProjectSingleton {
    val instance = UtilProject()
}

object AnotherClassSingleton {
    val instance = AnotherClass()
}

fun main(args: Array<String>) {
    UtilProject.f()
    println(AnotherClass.abc)

    val a = UtilProjectSingleton.instance // UtilProjectSingleton.instance will be initialized here.
    val b = AnotherClassSingleton.instance // AnotherClassSingleton.instance will be initialized here.

    val c = UtilProjectSingleton.instance // c is a.
}

如果您不关心每个单例是何时初始化的,您也可以这样使用它:

class UtilProject {
    ....
    companion object {
        fun f() = ...
    }
}

class AnotherClass {
    ...
    companion object {
        const val abc = "ABC"
    }
}

object Singletons {
    val utilProject = UtilProject()
    val anotherClass = AnotherClass()
}

fun main(args: Array<String>) {
    val a = Singletons.utilProject
    val b = Singletons.anotherClass 
}

In summary,
an object or a companion object is one singleton object in Kotlin.
You can assign variables in an object or objects, and then use the variables just like they were singletons.

objectcompanion object在首次使用时被实例化.

Kotlin相关问答推荐

Spring Boot Bean验证器未触发

映射中列表类型的Kotlin可空接收器?

我可以在kotlin/java.util.scanner中跳过分隔符而不重复吗?

如何定义一个函数来接受任何具有特定字段的数据类

Kotlin 复制列表中的项目以创建具有相同数据的不同对象的新列表

Kotlin 基于参数类型的返回类型推断

Kotlin 协程:flatMapLatest 什么都不发出

使用 Kotlin 的 Springboot 中缺少 ResponseEntity 正文属性

mutableStateOf 和 mutableStateListOf 有什么区别?

Kotlin 可空泛型

Kotlin 中获取类简单名称的最佳实践

Anko 中的水平线性布局

模拟异常 - 没有找到答案

将 Completable 转换为 Single 的规范方法?

在 Koin 中提供一个 Instance 作为其接口

Kotlin默认使用哪种排序?

空对象引用上的 TransitionSet ArrayList.size()

使用 Kotlin 创建自定义 Dagger 2 范围

用 kotlin 学习 Android MVVM 架构组件

Android studio,构建kotlin时出现奇怪错误:生成错误代码