注释

Last updated: ... / Reads: 35 Edit

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();
   */
}

文档注释

文档注释是以 /// 或 /** 开头的多行或单行注释。在连续行上使用 /// 与多行文档注释具有相同的效果。

在文档注释中,分析器会忽略所有文本,除非它包含在括号中。使用方括号,您可以引用类、方法、字段、顶级变量、函数和参数。括号中的名称在记录的程序元素的词法范围内解析。

以下是引用其他类和参数的文档注释示例:

/// 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:文档。


Comments

Make a comment