4 1










abstract class AbstractInstrument {

private $name;
private $category;
private $instruments = array();

public function add(AbstractInstrument $instrument) {
array_push($this->instruments, $instrument);
}

public function remove(AbstractInstrument $instrument) {
array_pop($this->instruments);
}

public function hasChildren() {
return (bool)(count($this->instruments) > 0);
}

public function getChild($i) {
return $instruments[$i];
}

public function getDescription() {
echo "- jeden " . $this->getName();
if ($this->hasChildren()) {
echo " złożony z:
";
foreach($this->instruments as $instrument) {
echo "
   -";
$instrument->getDescription();
echo "
";
}
}
}

public function setName($name) {
$this->name = $name;
}

public function getName() {
return $this->name;
}

public function setCategory($category) {
$this->category = $category;
}

public function getCategory() {
return $this->category;
}
}

class Guitar extends AbstractInstrument {
function __construct($name) {
parent::setName($name);
parent::setCategory("gitary");
}
}

class DrumSet extends AbstractInstrument {
function __construct($name) {
parent::setName($name);
parent::setCategory("perkusje");
}
}

class SnareDrum extends AbstractInstrument {
function __construct($name) {
parent::setName($name);
parent::setCategory("werble");
}
}

class BaseDrum extends AbstractInstrument {
function __construct($name) {
parent::setName($name);
parent::setCategory("bębny");
}
}

class Cymbal extends AbstractInstrument {
function __construct($name) {
parent::setName($name);
parent::setCategory("talerze");
}
}

$drums = new DrumSet("zestaw tama maple");
$drums->add(new SnareDrum("werbel"));
$drums->add(new BaseDrum("duży bęben"));

$cymbals = new Cymbal("zestaw talerzy zildjian");
$cymbals->add(new Cymbal("mały talerz"));
$cymbals->add(new Cymbal("duży talerz"));
$drums->add($cymbals);

$guitar = new Guitar("gibson les paul");

echo "Lista instrumentów:

";
$drums->getDescription();
$guitar->getDescription();

?>




Wyszukiwarka