PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php abstract class animal { static $id = 1; public $idAnimal = 0; abstract public function getOutputProduct(); } class chicken extends animal { function __construct() { $this->idAnimal = parent::$id++; } public function getOutputProduct() { return rand(0, 1); } } class cow extends animal { function __construct() { $this->idAnimal = parent::$id++; } // сколько может дать корова молоко public function getOutputProduct() { return rand(8, 12); } } class AnimalBreeding { public function addСhicken(): chicken { return new chicken(); } public function addCow(): cow { return new cow(); } } $stable = new AnimalBreeding(); $cow = []; $chicken = []; for ($i = 1; $i <= 10; $i++) { $cow[] = $stable->addCow(); } for ($i = 1; $i <= 20; $i++) { $chicken[] = $stable->addСhicken(); } print "Кур на ферме - " . count($chicken) . "\n"; print "Коров на ферме - " . count($cow) . "\n"; $milk = 0; $egg = 0; class WorkerHarvester { public $cows = 0; public $chickens = 0; public $milk = 0; public $egg = 0; function __construct($cows, $chickens) { $this->cows = $cows; $this->chickens = $chickens; } function getMilk($cows, $milk) { for ($i = 1; $i <= 7; $i++) { foreach ($cows as $value) { $milk += $value->getOutputProduct(); } } return $milk; } function getEgg($chickens) { for ($i = 1; $i <= 7; $i++) { foreach ($chickens as $value) { $egg += $value->getOutputProduct(); } return $egg; } } } $worker1 = new WorkerHarvester($cow, $chicken); //var_dump ($worker1->cows); $milk1 = 0; $milk1 += $worker1->getMilk($cow, $milk); $egg1 = 0; $egg1 += $worker1->getMilk($chicken, $egg); print "молока собрано " . $milk1 . "\n"; print "яиц собрано " . $egg1 . "\n";
Show:  
Copy Clear