因为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);
}