gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:44:58 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com