[TOC]
Dart支持單行注釋、多行注釋和文檔注釋。
## 單行注釋
單行注釋以//開頭。在//和行尾之間的所有內容都被Dart編譯器忽略。
~~~
void main() {
// TODO: refactor into an AbstractLlamaGreetingFactory?
print('Welcome to my Llama farm!');
}
~~~
## 多行注釋
多行注釋以/*開頭,以*/結尾。在/*和*/之間的所有內容都被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.
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) {
// ...
}
}
~~~
在生成的文檔中,[Food]成為了與Food類的API文檔的鏈接。
要解析Dart代碼并生成HTML文檔,可以使用[SDK的文檔生成工具]。有關生成文檔的示例,請參閱[Dart API文檔]。有關如何構造注釋的建議,請參閱[Dart文檔注釋的指南]。