вторник, 27 апреля 2021 г.

Composite

abstract class IShape {
  void getArea();
}

class Shape implements IShape {
  double area;

  Shape(this.area);

  @override
  void getArea() {
    print(area);
  }
}

class Rect extends Shape {
  double w, h;

  Rect(this.w, this.h) : super(w * h);
}

class Circle extends Shape {
  double radius;

  Circle(this.radius) : super(3.14 * radius * radius);
}

class Canvas implements IShape {
  var shapes = <IShape>[];

  @override
  void getArea() {
    for (var shape in shapes) {
      shape.getArea();
    }
  }
}

void main() {
  final canvas = Canvas();
  canvas.shapes = [
    Rect(5, 5),
    Circle(3),
  ];
  canvas.getArea();
}

Комментариев нет:

Отправить комментарий