Add the missing code to Hen and Egg so the following actions are completed:
- Hen implements the Fowl interface.
- A Hen lays an egg that will hatch into a new Hen.
- Eggs from other types of fowls should hatch into a new fowl of their parent type.
- Hatching an egg for the second time throws an Exceptions
<?php
interface Fowl {
public function layEgg() : Egg;
}
class Hen implements Fowl {
}
class Egg {
public function __construct(string $fowlType) {
}
public function hatch(): ?Fowl {
return null;
}
}
// Interface Gà
interface Fowl {
// Gà có hành động Ấp trứng
public function layEgg(): Egg;
}
// Con loại HEN (gà mái), kế thừa từ Trứng, thực thi hành động từ Interface
class Hen extends Egg implements Fowl {
public function layEgg() {
return new Hen();
}
}
// Class Trứng
class Egg {
private $hatchedCount = 0;
private $fowlType;
public function __construct(string $fowlType) {
$this->$fowlType = $fowlType;
}
public function hatch() :?Fowl {
$this->hatchedCount++;
if($this->hatchedCount > 1) {
throw new Exception();
}
switch($this->fowlType) {
case '':
return new Egg();
break;
default:
return new Egg();
break;
}
}
}
|