这是我使用 Kotlin 数据类创建器插件创建的数据类。
data class ResponseHealthInisghts(
val `data`: List<Data>,
val message: String,
val statusCode: Int
)
即使我删除了反引号,这段代码也能正常工作,我想知道它是否是为了 Java 互操作性。但是这个变量不是关键字,但它也有反引号。为什么?
基于 Why does this Kotlin method have enclosing backticks?
这个问题
is
是 Java 和 Kotlin 的关键字,但 数据 不是。
最佳答案
您可以简单地使用反引号将类、方法或变量名括起来
例如,如果有空格,这很有用:
class `Final Frontier` {
fun `out of space`() {
val `first second`: String?
}
}
或者正如你提到的,如果使用 Kotlin keywordIf a Java library uses a Kotlin keyword for a method
foo.`is`(bar)
data
是 Modifier Keyworddata instructs the compiler to generate canonical members for a class The following tokens act as keywords in modifier lists of declarations and can be used as identifiers in other contexts
和不是硬关键字不能用作标识符
The following tokens are always interpreted as keywords and cannot be used as identifier
关于android - 为什么 Kotlin 数据类对象有反引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58954821/