注释
Dart 支持单行注释、多行注释和文档注释。
单行注释
#单行注释以 //
开头。//
之后直到行尾的所有内容都会被 Dart 编译器忽略。
dart
void main() {
// TODO: refactor into an AbstractLlamaGreetingFactory?
print('Welcome to my Llama farm!');
}
多行注释
#多行注释以 /*
开头,以 */
结尾。/*
和 */
之间的所有内容都会被 Dart 编译器忽略(除非该注释是文档注释;请参阅下一节)。多行注释可以嵌套。
dart
void main() {
/*
* This is a lot of work. Consider raising chickens.
Llama larry = Llama();
larry.feed();
larry.exercise();
larry.clean();
*/
}
文档注释
#文档注释是多行或单行注释,以 ///
或 /**
开头。连续多行使用 ///
的效果与多行文档注释相同。
在文档注释中,分析器会忽略所有未用方括号括起来的文本。使用方括号可以引用类、方法、字段、顶级变量、函数和参数。方括号中的名称会在所文档化程序元素的词法作用域中解析。
以下是包含对其他类和参数引用的文档注释示例
dart
/// A domesticated South American camelid (Lama glama).
///
/// Andean cultures have used llamas as meat and pack
/// animals since pre-Hispanic times.
///
/// Just like any other animal, llamas need to eat,
/// so don't forget to [feed] them some [Food].
class Llama {
String? name;
/// Feeds your llama [food].
///
/// The typical llama eats one bale of hay per week.
void feed(Food food) {
// ...
}
/// Exercises your llama with an [activity] for
/// [timeLimit] minutes.
void exercise(Activity activity, int timeLimit) {
// ...
}
}
在类生成的文档中,[feed]
会成为指向 feed
方法文档的链接,而 [Food]
会成为指向 Food
类文档的链接。
要解析 Dart 代码并生成 HTML 文档,您可以使用 Dart 的文档生成工具 dart doc
。有关生成的文档示例,请参阅 Dart API 文档。有关如何组织注释的建议,请参阅 Effective Dart:文档。