编辑本页

编写文档

本资料英文原文的作者是 Aasmund Eldhuset;其所有权属于可汗学院(Khan Academy),授权许可为 CC BY-NC-SA 3.0 US(署名-非商业-相同方式共享)。请注意,这并不是可汗学院官方产品的一部分。中文版由灰蓝天际译,遵循相同授权方式。


Kotlin's documentation syntax is called KDoc. A KDoc block is placed above the construct it describes, and begins with /** and ends with */ (possibly on one line; if not, each intermediate lines should start with an aligned asterisk). The first block of text is the summary; then, you can use block tags to provide information about specific parts of the construct. Some block tags are @param for function parameters and generic type parameters, and @return for the return value. You can link to identifiers inside brackets. All the text outside of links and block tag names is in Markdown format.

/**
 * Squares a number.
 *
 * @param number Any [Double] number whose absolute value is
 * less than or equal to the square root of [Double.MAX_VALUE].
 * @return A nonnegative number: the result of multiplying [number] with itself.
 */
fun square(number: Double) = number * number

Package-level documentation can be provided in a separate Markdown file.

Unlike docstrings, KDoc blocks are not available to the program at runtime.

You can generate separate documentation files in HTML format from KDoc by using a tool called Dokka.


← 上一节:作用域内资源用法