註釋
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:文件。