科特林的单例课程

科特林的单例课程

本文介绍了科特林的单例课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道创建单例类的方法,这样我的Util类每个应用程序仅实例化一次.但是,当我将Java类转换为kotlin时,生成了以下代码.

I want to know way to create singleton class, so that my Util class instantiate only once per app. However when I converted my Java class to kotlin, below code was generated.

这正确吗?

companion object {
    private var utilProject: UtilProject? = null

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

我可以找到一个相关的 问题 ,但这是带有参数的,我是没有参数就无法转换.

I could find a related question, but it is with parameter, and I am not getting it convert without params.

推荐答案

只是

companion object {
    val instance = UtilProject()
}

可以完成此任务,因为伴侣对象本身是语言级别的单例.
(instance将在首先 调用伴随对象时分配.)

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

-更新-

如果需要调整何时初始化单例对象,则可以为每个类创建一个对象.

If you need to adjust when the singleton object should be initilized, 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的伴随对象时初始化它.为了避免在需要时进行初始化,可以这样使用:

Here, AnotherClass.instance is initialized before AnotherClass.instance is actually called. It is initialized when AnotherClass's companion object is called.To prevent being initialized before when needed, you can use like this:

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

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

If you don't care when each singleton is initialized, you can also use like this:

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
}

总而言之,
objectcompanion object是Kotlin中的一个单例对象.
您可以在 object objects 中分配变量,然后像使用单例一样使用变量.

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在首次使用时被实例化.第一次实例化object时(即,第一次使用object时)object中的valvar s初始化.

object or companion object is instantiated when it is first used.vals and vars in an object are initialized when the object is first instantiated (i.e., when the object is first used).

这篇关于科特林的单例课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:32