【Dart】入门05-命名构造函数

因为Dart中构造函数不能重载,所以才会有命名构造函数来实现类似重载的效果。

void main() {
  Graphical.withTriangle([3, 4, 5]);
  Graphical.withRectangle(10, 20);
}

//图形
class Graphical {
  List<double> _borders;
  //三角形
  Graphical.withTriangle(List<double> borders) {
    _borders = borders;
  }
  double _width, _height;
  //矩形
  Graphical.withRectangle(this._width, this._height);
}
发布了72 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_39370093/article/details/104311072