android - 为什么 Kotlin 数据类对象有反引号?

标签 android kotlin keyword backticks data-class

这是我使用 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 keyword

If a Java library uses a Kotlin keyword for a method

foo.`is`(bar)
dataModifier Keyword

data 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/

相关文章:

java - Android定制条形图

android - 如何使用 Bottom Sheet ,启用家长交互且可扩展

sql - 如何提取函数/过程中使用的所有 Oracle 关键字

c++ - 什么是 do(keyword) c++?

java - 从 Android 应用程序多次向 URL 发送请求的最佳方式是什么?

android - 将字符串中的字符替换为Image,然后设置为Textview

android - EditText中的DrawableButton可以显示/隐藏密码并更改DrawableEnd

java - 如何修复MainActivity? kotlin.UninitializedPropertyAccessException : lateinit property compositeDiposable has not been initialized

programming-languages - 语句和关键字有什么区别?

android - 非 Activity 时如何访问窗口?