@@ -1729,9 +1729,6 @@ renderLargeRectangles($rectangles);
1729
1729
``` php
1730
1730
abstract class Shape
1731
1731
{
1732
- protected $width = 0;
1733
- protected $height = 0;
1734
-
1735
1732
abstract public function getArea(): int;
1736
1733
1737
1734
public function render(int $area): void
@@ -1742,13 +1739,12 @@ abstract class Shape
1742
1739
1743
1740
class Rectangle extends Shape
1744
1741
{
1745
- public function setWidth(int $width): void
1746
- {
1747
- $this->width = $width;
1748
- }
1742
+ private $width;
1743
+ private $height;
1749
1744
1750
- public function setHeight (int $height): void
1745
+ public function __construct (int $width, int $ height)
1751
1746
{
1747
+ $this->width = $width;
1752
1748
$this->height = $height;
1753
1749
}
1754
1750
@@ -1760,9 +1756,9 @@ class Rectangle extends Shape
1760
1756
1761
1757
class Square extends Shape
1762
1758
{
1763
- private $length = 0 ;
1759
+ private $length;
1764
1760
1765
- public function setLength (int $length): void
1761
+ public function __construct (int $length)
1766
1762
{
1767
1763
$this->length = $length;
1768
1764
}
@@ -1779,19 +1775,12 @@ class Square extends Shape
1779
1775
function renderLargeRectangles(array $rectangles): void
1780
1776
{
1781
1777
foreach ($rectangles as $rectangle) {
1782
- if ($rectangle instanceof Square) {
1783
- $rectangle->setLength(5);
1784
- } elseif ($rectangle instanceof Rectangle) {
1785
- $rectangle->setWidth(4);
1786
- $rectangle->setHeight(5);
1787
- }
1788
-
1789
1778
$area = $rectangle->getArea();
1790
1779
$rectangle->render($area);
1791
1780
}
1792
1781
}
1793
1782
1794
- $shapes = [new Rectangle(), new Rectangle(), new Square()];
1783
+ $shapes = [new Rectangle(4, 5 ), new Rectangle(4, 5 ), new Square(5 )];
1795
1784
renderLargeRectangles($shapes);
1796
1785
```
1797
1786
0 commit comments