From 153dc5c95a509461170a15fef9ccdfd51546bc71 Mon Sep 17 00:00:00 2001 From: locknd Date: Fri, 30 Dec 2022 20:38:48 +0500 Subject: [PATCH] Exercise 4 --- exercise 4/exercise 4.php.txt | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 exercise 4/exercise 4.php.txt diff --git a/exercise 4/exercise 4.php.txt b/exercise 4/exercise 4.php.txt new file mode 100644 index 0000000..a6c0244 --- /dev/null +++ b/exercise 4/exercise 4.php.txt @@ -0,0 +1,64 @@ +// Абстрактный класс для фигур +abstract class Shape { + abstract public function getArea(); + abstract public function getPerimeter(); +} +// Интерфейс для фигур с периметром +interface Perimeter { + public function getPerimeter(); +} + +// Квадрат +class Square extends Shape implements Perimeter { + private $sideLength; + + public function __construct($sideLength) { + $this->sideLength = $sideLength; + } + + public function getArea() { + return pow($this->sideLength, 2); + } + + public function getPerimeter() { + return $this->sideLength * 4; + } +} + +// Прямоугольник +class Rectangle extends Shape implements Perimeter { + private $length; + private $width; + + public function __construct($length, $width) { + $this->length = $length; + $this->width = $width; + } + + public function getArea() { + return $this->length * $this->width; + } + + public function getPerimeter() { + return 2 * ($this->length + $this->width); + } +} + +// Круг +class Circle extends Shape { + private $radius; + + public function __construct($radius) { + $this->radius = $radius; + } + + public function getArea() { + return pi() * pow($this->radius, 2); + } + + // Периметр неприменим к кругу, поэтому оставляем его пустым + public function getPerimeter() {} +} + +// Треугольник +class Triangle extends Shape implements