以下代码段中使用的反引号是什么?
为什么要在 fun is(amount:Int):Boolean { ... }
周围添加它们?
verifier.`is`(amount)
最佳答案
这是因为 is
是 Kotlin 中的保留关键字。由于 Kotlin 应该与 Java 互操作,并且 is
是 Java 中的有效方法(标识符)名称,因此反引号用于转义该方法,以便它可以用作方法而不会混淆它一个关键字。没有它,它将无法工作,因为它是无效的语法。
这是突出显示 in the Kotlin documentation :
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 (`) characterfoo.`is`(bar)
关于kotlin - 为什么这个 Kotlin 方法有封闭的反引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149474/