Advertisement
HristoBaychev

cars

Apr 20th, 2023
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.67 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Създайте набор от класове, които моделират различни видове превозни средства, като автомобили, камиони и мотоциклети.
  5. Използвайте наследяване за да дефинирате родителски клас превозно средство с общи свойства и методи и след това създайте дъщерни класове
  6. за всеки конкретен тип превозно средство, които наследяват от класа Vehicle i добавят свои собствени никални свойства и методи.
  7.  
  8. Изисквания:
  9. 1.Класът Vehicle трябва да има prooerties за марка, модел, година и цвят, както и методи за получавайе и настройка за всяко от тях.
  10.  
  11. 2.Всеки дъщерен клас трябва да има свои собствени уникални свойства и методи, които са специжични за този тип превозно средство.
  12. Нпаример класът кар може да има свойства за брой врати и капацитет на багажника,
  13. докато класът мотор може да има свойства за брой колела и размер на двигателя.
  14.  
  15. 3. Всеки дъщерен клас трябва да наследява атрибутите и методите на на класа Vehicle и трябва да може да осъществява
  16. достъп до тях и да ги модифицира с помощта на подходящите модификатори за достъп. - ОК
  17.  
  18. 4. Създайте поне едно копие на всеки дъщерен клас и демонстрирайте как може да се използва за достъп до свойства
  19. и методите както на дъщерния така и на родителксия клас. - ok
  20.  
  21. 5. Дефинирайте константи във финал клас, които да бъдат марките превозни средства. Потърсете сами как се създава такъв enum class - ok
  22.  
  23.  
  24. Създаване на класове:
  25. 1. Vehicle - основен - ok
  26. 2. car - ok
  27. 3. Truck - ok
  28. 4. Motocykle - ok
  29.  
  30. какво трябва да има един автомобил като основа:
  31. Марка : brand - ok
  32. Година : yearofmade - ok
  33. Модел : model - ok
  34. Цвят : color - ok
  35.  
  36. дъщерни класове- кола
  37. 1. брой врати - ok
  38. 2. двигател - ok
  39. 3. капацитет багажник - ok
  40. камион -
  41. 1. брой врати - ok
  42. 2. двигател - ok
  43. 3. ремарке - ok
  44. морор -
  45. 1. двигател - ok
  46. 2. брой колела - ok
  47. 3. литри резервоар - ok
  48.  
  49.  
  50. */
  51.  
  52.  enum CarBrand{
  53.     const BMW = "BMW";
  54.     const AUDI = "AUDI";
  55.     const SIMSON = "SIMSON";
  56.  }
  57.  
  58.  
  59.  class Vehicle{
  60.     protected $brand;
  61.     protected $year;
  62.     protected $model;
  63.     protected $color;
  64.  
  65.     public function __construct(string $brand, int $year, string $model, string $color)
  66.     {
  67.         $this->brand = $brand;
  68.         $this->year = $year;
  69.         $this->model = $model;
  70.         $this->color = $color;
  71.     }
  72.  
  73.     public function getBrand(){
  74.         return $this->brand;
  75.     }
  76.     public function getYear(){
  77.         return $this->year;
  78.     }
  79.     public function getModel(){
  80.         return $this->model;
  81.     }
  82.     public function getColor(){
  83.         return $this->color;
  84.     }
  85.  
  86.     public function setBrand($brand){
  87.         $this->brand = $brand;
  88.     }
  89.     public function setYear($year){
  90.         $this->year = $year;
  91.     }
  92.     public function setModel($model){
  93.         $this->model = $model;
  94.     }
  95.     public function setColor($color){
  96.         $this->color = $color;
  97.     }
  98.  
  99.  }
  100.  
  101.  class Car extends Vehicle{
  102.     private $carDoors;
  103.     private $carEngine;
  104.     private $carCapacity;
  105.  
  106.     public function __construct(string $brand, int $year, string $model, string $color, int $carDoors, float $carEngine, int $carCapacity){
  107.         parent::__construct($brand, $year, $model, $color);
  108.         $this->carDoors = $carDoors;
  109.         $this->carEngine = $carEngine;
  110.         $this->carCapacity = $carCapacity;
  111.     }
  112.     public function getCarDoors(){
  113.         return $this->carDoors;
  114.     }
  115.     public function getCarEngine(){
  116.         return $this->carEngine;
  117.     }
  118.     public function getCarCapacity(){
  119.         return $this->CarCapacity;
  120.     }
  121.  
  122.     public function setCarDoors($carDoors){
  123.         $this->carDoors = $carDoors;
  124.     }
  125.     public function setCarEngine($carEngine){
  126.         $this->carEngine = $carEngine;
  127.     }
  128.     public function setCarCapacity($carCapacity){
  129.         $this->CarCapacity = $carCapacity;
  130.     }
  131.  
  132.  }
  133.  
  134.  class Truck extends Vehicle{
  135.     private $truckDoor;
  136.     private $truckEngine;
  137.     private $truckTrailer;
  138.  
  139.     public function __construct(string $brand, int $year, string $model, string $color, int $truckDoor, float $truckEngine, int $truckTrailer){
  140.         parent::__construct($brand, $year, $model, $color);
  141.         $this->truckDoor = $truckDoor;
  142.         $this->truckEngine = $truckEngine;
  143.         $this->truckTrailer = $truckTrailer;
  144.     }
  145.  
  146.     public function getTruckDoor(){
  147.         return $this->truckDoor;
  148.     }
  149.     public function getTruckEngine(){
  150.         return $this->truckEngine;
  151.     }
  152.     public function getTruckTrailer(){
  153.         return $this->truckTrailer;
  154.     }
  155.  
  156.     public function setTruckDoor($truckDoor){
  157.         $this->truckDoor = $truckDoor;
  158.     }
  159.     public function setTruckEngine($truckEngine){
  160.         $this->truckEngine = $truckEngine;
  161.     }
  162.     public function setTruckTrailer($truckTrailer){
  163.         $this->truckTrailer = $truckTrailer;
  164.     }
  165.  
  166.  }
  167.  
  168.  class Motocykle extends Vehicle{
  169.     private $motEngine;
  170.     private $motTyre;
  171.     private $motTank;
  172.     public function __construct(string $brand, int $year, string $model, string $color, $motEngine, $motTyre, $motTank){
  173.         parent::__construct($brand, $year, $model, $color);
  174.         $this->motEngine = $motEngine;
  175.         $this->motTyre = $motTyre;
  176.         $this->motTank = $motTank;
  177.     }
  178.  
  179.     public function getMotEngine(){
  180.         return $this->motEngine;
  181.     }
  182.     public function getMotTyre(){
  183.         return $this->motTyre;
  184.     }
  185.     public function getMotTank(){
  186.         return $this->motTank;
  187.     }
  188.  
  189.     public function setMotEngine($motEngine){
  190.         $this->motEngine = $motEngine;
  191.     }
  192.     public function setMotTyre($motTyre){
  193.         $this->motTyre = $motTyre;
  194.     }
  195.     public function setMotTank($motTank){
  196.         $this->motTank = $motTank;
  197.     }
  198.  
  199.  }
  200.  
  201.  
  202. $car = new Car(CarBrand::BMW, 2022, 'M5', 'Red', 2, 5.0, 4);
  203.  
  204. echo "Brand car is: " . $car->getBrand().PHP_EOL;
  205. echo "Car model is: " . $car->getModel().PHP_EOL;
  206. echo "Car door is: " . $car->getCarDoors().PHP_EOL;
  207. echo "Car engine is: " . $car->getCarEngine().PHP_EOL;
  208. echo "Car color is: " . $car->getColor().PHP_EOL;
  209.  
  210. $car->setColor('Blue').PHP_EOL;
  211. $car->setCarCapacity(2).PHP_EOL;
  212. echo "New color car is: " . $car->getColor().PHP_EOL;
  213. echo "New car capacity is: " . $car->getCarCapacity().PHP_EOL;
  214. echo PHP_EOL;
  215. echo PHP_EOL;
  216.  
  217. /*--------------------------------------------------------------------------------------------*/
  218.  
  219. $truck = new Truck(CarBrand::AUDI, 2020, 'FH16', 'White', 2, 13.0, 3);
  220.  
  221. echo "Truck brand is: " . $truck->getBrand().PHP_EOL;
  222. echo "Trucl trailer is: " . $truck->getTruckTrailer().PHP_EOL;
  223. echo "Truck engine is: " . $truck->getTruckEngine().PHP_EOL;
  224. echo "Truck color is: " .  $truck->getColor().PHP_EOL;
  225.  
  226. $truck->setYear(2021).PHP_EOL;
  227. $truck->setTruckDoor(3).PHP_EOL;
  228. echo "New truck year is: " . $truck->getYear().PHP_EOL;
  229. echo "New truck door is: " . $truck->getTruckDoor().PHP_EOL;
  230. echo PHP_EOL;
  231. echo PHP_EOL;
  232. /*--------------------------------------------------------------------------------------------*/
  233.  
  234. $moto = new Motocykle(CarBrand::SIMSON, 2023, 'CBR1000RR', 'Black', 1000, 17, 18);
  235.  
  236. echo "Motor Brand is: " . $moto->getBrand().PHP_EOL;  
  237. echo "Motor engine is: " . $moto->getMotEngine().PHP_EOL;
  238. echo "Motor tank capacity is: " . $moto->getMotTank().PHP_EOL;
  239.  
  240. $moto->setMotEngine(750).PHP_EOL;
  241. $moto->setMotTyre(18).PHP_EOL;
  242. echo "New motor engine is: " . $moto->getMotEngine().PHP_EOL;
  243. echo "New motor tyre is: " . $moto->getMotTyre().PHP_EOL;
  244. /*--------------------------------------------------------------------------------------------*/
  245.  
  246.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement