为什么这个Kotlin方法有封闭反引号? [英] Why does this Kotlin method have enclosing backticks?
问题描述
下面的代码段中使用的反引号有哪些?
What are the backticks used for in the snippet below?
为什么要在附近添加乐趣(金额:Int):布尔{...}
?
verifier.`is`(amount)
推荐答案
这是因为
是Kotlin中的保留关键字。由于Kotlin应该可以与Java互操作并且
是Java中的有效方法(标识符)名称,因此反引号用于转义方法,以便它可以用作一种方法,不会将其混淆为关键字。没有它它将无法工作,因为它将是无效的语法。
It's because is
is a reserved keyword in Kotlin. Since Kotlin is supposed to be interoperable with Java and is
is a valid method (identifier) name in Java, the backticks are used to escape the method so that it can be used as a method without confusing it as a keyword. Without it it will not work because it would be invalid syntax.
这是突出显示的:
This is highlighted in the Kotlin documentation:
某些Kotlin关键字是Java中的有效标识符:在
,对象
,是
等等。如果Java库使用Kotlin关键字一个方法,你仍然可以调用用反引号(`)字符转义它的方法
Escaping for Java identifiers that are keywords in Kotlin
Some of the Kotlin keywords are valid identifiers in Java:
in
,object
,is
, etc. If a Java library uses a Kotlin keyword for a method, you can still call the method escaping it with the backtick (`) character
foo.`is`(bar)
这篇关于为什么这个Kotlin方法有封闭反引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!