內容

註解

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 文件。 有關如何組織註解的建議,請參閱 有效的 Dart:文件。