Dart中的注释
注释
Dart 支持单行注释、多行注释和文档注释,跟别的语言略微有点区别,例如: ///
是文档注释,具体如下,一起学习下。
参考:
Dart中的注释
单行注释
单行注释以 //
开始。所有在 //
和该行结尾之间的内容均被编译器忽略。
void main() {
// TODO: refactor into an AbstractLlamaGreetingFactory?
print('Welcome to my Llama farm!');
}
多行注释
多行注释以 /*
开始,以 */
结尾。所有在 /*
和 */
之间的内容均被编译器忽略(不会忽略文档注释),多行注释可以嵌套。
void main() {
/*
* This is a lot of work. Consider raising chickens.
Llama larry = Llama();
larry.feed();
larry.exercise();
larry.clean();
*/
}
文档注释
文档注释可以是多行注释,也可以是单行注释,文档注释以 ///
或者 /**
开始。在连续行上使用 ///
与多行文档注释具有相同的效果。
在文档注释中,除非用中括号括起来,否则分析器会忽略所有文本。使用中括号可以引用类、方法、字段、顶级变量、函数和参数。括号中的符号会在已记录的程序元素的词法域中进行解析。
下面是一个引用其他类和成员的文档注释:
/// 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
类的 API 文档。
解析 Dart 代码并生成 HTML 文档,可以使用 Dart 的文档生成工具 dart doc
。关于生成文档的示例,请参考 Dart API documentation 查看关于文档结构的建议,请参考文档: Guidelines for Dart Doc Comments.。