任何飞镖专家都说明下面的代码是什么飞镖构造函数?

时间:2020-03-31 16:49:35

标签: dart

谁能解释为什么我们在构造函数的参数中使用大括号。

class Cars {
  String carName;
  bool isAuto;

  // create the constructor
  Cars({String honda, bool yes}) {
    carName = honda;
    isAuto = yes;
  }
}

1 个答案:

答案 0 :(得分:3)

它被命名为参数。

用于创建实例:

Cars(honda: 'foo', yes: true);
// or 
Cars(yes: true, honda: 'foo');

如果您不使用卷曲,将是:

class Cars {
  String carName;
  bool isAuto;

  // create the constructor
  Cars(String honda, bool yes) {
    carName = honda;
    isAuto = yes;
  }
}

然后您将按顺序创建一个新实例:

Cars('foo', true);

此外,您可以自动初始化:

class Cars {
  String carName;
  bool isAuto;

  Cars(this.carName, this.isAuto);
}