0

I'm trying to reach a constant String value of the Class name. But I really don't understand why I get a modified String value. Here is the code that I'm working on:

class TestClass {
    companion object {
            @JvmField
            val TAG1: String = this::class.java.name as String
            val TAG2: String = this::javaClass.name 
    } 
}

In another class trying to reach the value like this:

class ComboClass {
    override fun onStart() {
       val tag1 = TestClass.TAG1
       val tag2 = TestClass.TAG2

       // tag1 "packagePath.TestClass$Companion"
       // tag2 "packagePath.TestClass$Companion"
    }
}

Why Am I getting packagePath.TestClass$Companion at the end of the String value? I'm expecting to get packagePath.TestClass

Thanks

4

1 回答 1

2

为什么我在字符串值的末尾得到 packagePath.TestClass$Companion?

伴生对象有自己的类,在这种情况下,对象是 type TestClass$Companion,所以this::class.java.name as Stringthis::javaClass.name都给你伴生对象的类的名称。

如何获取测试类

你会想要使用TestClass::class.java.nameTestClass::javaClass.name不是得到TestClass

于 2018-01-27T15:32:07.093 回答