我对 OOP 很陌生,并试图弄清楚。我有一个哺乳动物类的以下代码,我计划进一步开发。我有一个与正确值相呼应的 Bear 子类,但我无法覆盖 Grizzly 子类中的 $name、$move、$ear 或 $sound 值。
abstract class Mammal
{
protected $name;
protected $limbs;
protected $offspring = 'live';
protected $move;
protected $eat;
protected $sound;
protected function __construct($name, $limbs, $offspring, $move, $eat, $sound) {
$this->name = $name;
$this->limbs = $limbs;
$this->offspring = $offspring;
$this->move = $move;
$this->eat = $eat;
$this->sound = $sound;
}
public function getOutput() {
echo "The {$this->name} has four {$this->limbs}. The offspring is birthed {$this->offspring} and move by {$this->move}. They eat {$this->eat} and talk by {$this->sound}.";
}
}
class Bear extends Mammal
{
public function __construct() {
Mammal::__construct('bear', 'claws', $this->offspring, '', '', '');
}
}
class Grizzly extends Bear
{
public function __construct() {
Bear::__construct('grizzly bear', 'claws', $this->offspring, 'lumbering', 'salmon', 'roaring');
}
}
$grizzly = new Grizzly;
$grizzly->getOutput();
我想要得到的输出是:“灰熊有四个爪子。后代是通过伐木而活下来的。他们吃鲑鱼,用咆哮说话。” 我很感激任何帮助!