PHP子类不会覆盖新值

时间:2013-10-12 00:45:12

标签: php oop

我对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();

我想要得到的结果是:“灰熊有四只爪子。后代是现场出生,笨拙地移动。他们吃鲑鱼,咆哮说话。”我感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

原因是你的熊类似乎没有变量。


class Bear extends Mammal
{
    public function __construct() { //See, this constructor takes nothing
        Mammal::__construct('bear', 'claws', $this->offspring, '', '', '');
    }
}

这是我为使其发挥作用而做的事情



class Bear extends Mammal
{
    public function __construct($bear = 'bear') {//right hear
        Mammal::__construct($bear, 'claws', $this->offspring, '', '', '');
    }
}

Your code in codepad.org with my little change

注意:这就是我的工作方式

答案 1 :(得分:0)

问题出在Bear的设计中。您希望您的继承链不阻止您需要的值。也就是说,你没有 来调用父构造函数......

class Grizzly extends Bear
{
    public function __construct() {
        Mammal::__construct('grizzly bear', 'claws', $this->offspring, 'lumbering', 'salmon', 'roaring');
    }
}

像我在这里所做的那样跳过父构造函数实际上并不是很好的OOP,但它有效(并且PHP的哲学价值观这样实用主义)。但是,在这种情况下,您可能希望修改Bear子类以启用对这些属性的访问。

class Bear extends Mammal
{
    public function __construct($type, $move, $eat, $sound) {
        parent::__construct($type . ' bear', 'claws', $this->offspring, $move, $eat, $sound);
    }
}

class Polar extends Bear
{
    public function __construct() {
        parent::__construct('polar', 'cross-country skiing', 'salmon', 'mewling');
    }
}

$polar = new Polar();
$polar->getOutput();
  北极熊有四只爪子。后代是现场生产,并通过越野滑雪移动。他们吃鲑鱼,并通过喵喵叫说话。

答案 2 :(得分:0)

我的想法:

如果你正在创造动物物种,那么熊应该抽象。您不应该创建一个Bear,就像您无法创建一个哺乳动物一样。

Bear和Grizzly熊也有属性。

abstract class Bear extends Mammal
{
    protected $limbs = 'claws';
    protected $move = 'lumbering';
    protected $sound = 'roaring';
}

所有熊都有爪子,木材和咆哮

class Grizzly extends Bear
{
    protected $name = 'Grizzly Bear';
    protected $eat = 'salmon';
}

灰熊吃三文鱼

abstract class Mammal
{
    protected $name;
    protected $limbs;
    protected $offspring = 'live';
    protected $move;
    protected $eat;
    protected $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}.";
    }
}

这将是我的解决方案。我们一起摆脱了一个构造函数。