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; public abstract 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=array(); $chicken=array(); 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"; //var_dump($chicken); // обнуляем корзину //$milk=0; //$egg=0; class WorkerHarvester { public $cows; public $chickens; public function setCows() { return $this->cows; } public function setChickens() { return $this->chickens; } public $milk; public $egg; public function HarvestMilk(){ for ($i=1; $i<=7; $i++) { foreach ($cows as $value){ $milk +=$value->getOutputProduct(); return @milk; } } } public function HarvestEgg(){ for ($i=1; $i<=7; $i++) { foreach ($chickens as $value){ $egg +=$value->getOutputProduct(); return @egg; } } } } $worker1 = new WorkerHarvester(); $worker1 -> setCows = $cow; $worker1 -> setChikens = $chiken; $worker1 -> HarvestMilk; print "Молока собрали за неделю - ".$milk."\n";
Show:  
Copy Clear