Object 如何知道对象是否是Kotlin中的单例?

Object 如何知道对象是否是Kotlin中的单例?,object,reflection,kotlin,singleton,tostring,Object,Reflection,Kotlin,Singleton,Tostring,我想在超类中定义toString(),以便它检测到类实例是一个单例(一个对象)并打印它的名称 在科特林有可能吗 的以下属性可能会有所帮助: /** * The instance of the object declaration, or `null` if this class is not an object declaration. */ public val objectInstance: T? 下面是一个例子: object Singleton println(Singleton

我想在超类中定义
toString()
,以便它检测到类实例是一个单例(一个
对象
)并打印它的名称

在科特林有可能吗

的以下属性可能会有所帮助:

/**
 * The instance of the object declaration, or `null` if this class is not an object declaration.
 */
public val objectInstance: T?
下面是一个例子:

object Singleton

println(Singleton::class.objectInstance) // xx.Singleton@77a57272
println(""::class.objectInstance) //null

答案有用吗?