kotlin - 为什么这个 Kotlin 方法有封闭的反引号?

以下代码段中使用的反引号是什么?

为什么要在 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 (`) character

foo.`is`(bar)

https://stackoverflow.com/questions/44149474/

相关文章:

android - 无法获取提供商 com.google.android.gms.ads.Mobil

android - 如何在 Kotlin 中获取当前的本地日期和时间

kotlin - 传递 null 时,有没有办法在非可选参数上使用默认值?

java - Android Studio 3.1.3 - Unresolved reference

kotlin - 在 Kotlin 中复制 map 最聪明的方法是什么?

android - Dagger 2 与 Kotlin 的多重绑定(bind)

android - 在 Kotlin 中的原始类型属性上不允许使用 lateinit 修饰符

android - 在 android studio 中运行带有 main 函数的单个 kotlin

java - 如何将 Java 赋值表达式转换为 Kotlin

android - Kotlin 等价于 Java 的 equalsIgnoreCase