📜  dart 在构造函数中设置最终变量 - Dart 代码示例

📅  最后修改于: 2022-03-11 14:48:02.277000             🧑  作者: Mango

代码示例1
// You cannot mutate final variables in a constructor body, 
// instead you must use a special syntax.
// Also note if you have a super() call, it must be called last.
class Point {
  final num x;
  final num y;
  final num distanceFromOrigin;

  // Special syntax
  Point(this.x, this.y) :
    distanceFromOrigin = sqrt(pow(x, 2) + pow(y, 2));
}