如何在Kotlin声明Singleton实例,它与我们宣布Singleton班级的方式相似吗?

发布于 2025-02-11 05:39:15 字数 215 浏览 2 评论 0原文

在一次采访中,我被告知要写一个单人课,所以我写了以下代码

  object Ant{

    }

,但后来他要求我写Singleton实例,让我感到困惑,我

  object what{

   }

现在像这样写了这样的书,我知道我是错的,但我真的很好奇如何写下Singleton实例。

In an interview I was told to write a Singleton class so I wrote the following code

  object Ant{

    }

but later he asked me to write Singleton instance which confused me and I ended up writing like this

  object what{

   }

now, I know I am wrong but I am really curious how to write down Singleton Instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2025-02-18 05:39:15

请检查我的助手类,我使用的可能是错误的,所以请纠正我

class Helper{

    companion object{

    @Volatile
    var INSTANCE : Helper? = null

    fun getInstance(): Helper {
        return INSTANCE?: synchronized(this){
            val instance = Helper()
            INSTANCE = instance
            instance
        }
    }

  }

}

,然后我会创建一个val helper = helper.getInstance()的变量,并从那时起使用此对象,有时我将它们声明为外部的全局变量,以使最近在应用程序中更容易访问

我们已转移到Koin,因此我们只使用@single ksp注释将这些类称为singleton

please check my helper class, What I was using might be wrong so please correct me

class Helper{

    companion object{

    @Volatile
    var INSTANCE : Helper? = null

    fun getInstance(): Helper {
        return INSTANCE?: synchronized(this){
            val instance = Helper()
            INSTANCE = instance
            instance
        }
    }

  }

}

and then I would create a variable like this val helper = Helper.getInstance() and use this object from then on, sometimes I declare them as global variables outside class to make it easier to access across the app

recently we have shifted to Koin so we just declare these classes as singleton by using @Single ksp annotation

梦晓ヶ微光ヅ倾城 2025-02-18 05:39:15

事件发生后,很难猜测面试官在问什么,但是在上面的代码中,ant> ant什么什么什么 >但是,只有对象声明。您可能需要像以下方式实例化对象:

val mySingleton = What

对象声明为首次使用时懒洋洋

It is hard to guess what an interviewer was asking about, after the event, but in your code above there is no instance of either Ant or What yet, only the object declaration. You would need to instantiate the object like:

val mySingleton = What

Object declarations are initialized lazily when first used

清欢 2025-02-18 05:39:15

我认为你是对的!

在kotlin中,对象 a singleton:它定义了一个类,用一个实例。 (该实例是自动创建的,您无法创建任何其他实例。)因此,如果您需要单身人士,那是获得它的标准方法。

可以做类似的事情:

class Ant private constructor() {
    companion object {
        val INSTANCE = Ant()
    }
}

这就是您必须在Java:一个私有构造函数和一个静态实例中进行操作。您的面试官可能更熟悉Java,并且期望您这样做 - 但这在Kotlin中绝对毫无意义,因为object做得完全相同,只有更简单的事情(并且更少的机会错误或种族条件或其他)。

如果您使用的是诸如Spring之类的框架,它提供了创建单个实例的其他方法(例如,带有@component@Bean等的注释),但是这些是特定于框架,因此您可能知道您是否需要类似的东西。

或者您的面试官可能会想到其他事情 - 但是在这种情况下,他应该给您一些暗示。您不是一个心灵阅读者!

最重要的是,如果没有任何更多信息,object是获得单身人士的最明显方法,因此从您发布的内容来看,您是对的。

I think you were right!

In Kotlin, an object is a singleton: it defines a class, with exactly one instance. (That one instance is created automatically, and you can't create any others.) So if you need a singleton, that's the standard way to get it.

You could do something like:

class Ant private constructor() {
    companion object {
        val INSTANCE = Ant()
    }
}

That's how you'd have to do it in Java: a private constructor and a single static instance. It may be that your interviewer was more familiar with Java, and was expecting you to do that — but that's absolutely pointless in Kotlin, because object does exactly the same, only more concisely (and with less opportunity for errors or race conditions or whatever).

If you were using a framework such as Spring, that provides other ways of creating single instances (e.g. with annotations such as @Component, @Bean, etc.) but those are specific to the framework, so you'd probably know if you needed anything like that.

Or your interviewer may have something else in mind — but in that case, he should have given you some hints; you're not a mind-reader!

The bottom line is that without any further information, object is the most obvious way to get a singleton, so from what you've posted, you were right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文