Kotlin 简明教程
Kotlin - Comments
注释是 Kotlin 源代码中面向编程人员的说明或批注。添加注释的目的是使人员更能简单地了解源代码,Kotlin 编译器忽略这些注释。
A comment is a programmer-readable explanation or annotation in the Kotlin source code. They are added with the purpose of making the source code easier for humans to understand, and are ignored by Kotlin compiler.
与大多数现代语言一样,Kotlin 支持单行(或行尾)注释和多行(块)注释。Kotlin 注释与 Java、C 和 C++ 编程语言中可用的注释类似。
Just like most modern languages, Kotlin supports single-line (or end-of-line) and multi-line (block) comments. Kotlin comments are very much similar to the comments available in Java, C and C++ programming languages.
Kotlin Single-line Comments
Kotlin 中的单行注释以两个正斜杠 // 开始,以行尾结束。因此 Kotlin 编译器会忽略 // 和行尾之间编写的任何文本。
Single line comments in Kotlin starts with two forward slashes // and end with end of the line. So any text written in between // and the end of the line is ignored by Kotlin compiler.
下面是使用单行注释的 Kotlin 程序示例:
Following is the sample Kotlin program which makes use of a single-line comment:
// This is a comment
fun main() {
println("Hello, World!")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Hello, World!
单行注释可从程序的任何位置开始,并且将一直持续到行尾。例如,你可以使用以单行注释如下所示:
A single line comment can start from anywhere in the program and will end till the end of the line. For example, you can use single line comment as follows:
fun main() {
println("Hello, World!") // This is also a comment
}
Kotlin Multi-line Comments
Kotlin 中的多行注释以 / 开始,以 / 结束。因此 / * 和 */ 之间编写的任何文本将被视为注释,并且 Kotlin 编译器会忽略这些文本。
A multi-line comment in Kotlin starts with / and end with /. So any text written in between /* and */ will be treated as a comment and will be ignored by Kotlin compiler.
多行注释在 Kotlin 中也称为“块”注释。
Multi-line comments also called Block comments in Kotlin.
下面是使用多行注释的 Kotlin 程序示例:
Following is the sample Kotlin program which makes use of a multi-line comment:
/* This is a multi-line comment and it can span
* as many lines as you like
*/
fun main() {
println("Hello, World!")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Hello, Word!
Kotlin Nested Comments
Kotlin 中的块注释可以嵌套,这意味着单行注释或多行注释可以位于多行注释中,如下所示:
Block comments in Kotlin can be nested, which means a single-line comment or multi-line comments can sit inside a multi-line comment as below:
/* This is a multi-line comment and it can span
* as many lines as you like
/* This is a nested comment */
// Another nested comment
*/
fun main() {
println("Hello, World!")
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Hello, World!