为什么这个 Kotlin 方法有封闭的反引号? [英] Why does this Kotlin method have enclosing backticks?

查看:21
本文介绍了为什么这个 Kotlin 方法有封闭的反引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面代码段中的反引号有什么用?

What are the backticks used for in the snippet below?

为什么在 fun is(amount:Int ):Boolean { ... } 周围添加它们?

Why add them around the fun is(amount:Int ):Boolean { ... }?

verifier.`is`(amount)

推荐答案

这是因为 is 在 Kotlin 中是一个保留关键字.由于 Kotlin 应该可以与 Java 互操作,并且 is 是 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.

这是突出显示的在 Kotlin 文档中:

一些 Kotlin 关键字是 Java 中的有效标识符:inobjectis 等.如果 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆