如何在oop PHP中将参数传递给父构造函数?

时间:2019-04-26 04:38:04

标签: php oop inheritance

我试图实例化一个基类的两个对象,并从子类构造函数将参数传递给父类构造函数。这似乎不起作用。它的作用就好像对象甚至不是父类的子对象。我不知道我在做什么错。

我尝试重新排列包含项,不传递任何参数(这会导致错误,“期望一个参数”),并且其他修改都无济于事。

父类:

class SuperHero {
    private $health;
    private $name;
    private $isDead;

    public function __construct($name) {
        $this->name;
        $this->isDead = false;
    }

    // ...

    public function attack($opponent) { 
        echo $this->name.' attacks '.$opponent->name.'!<br>';
        $dmg = mt_rand(1, 10);
        $opponentHealth = determineHealth($opponent, $dmg);
        echo $opponent->name.' has '.$opponentHealth.' health left!<br>';
    }

    // ...

子类:

<?php
require_once('SuperHero.php');
class Batman extends SuperHero {

    public function __construct() {
        parent::__construct('Batman');
        $this->health = mt_rand(1, 1000);
    }

}

执行的脚本:

require_once('Batman.php');
require_once('Superman.php');

$h1 = new Batman;
$h2 = new Superman;

echo $h1->name.' is starting with '.$h1->health.' health!<br>';
echo $h2->name.' is starting with '.$h2->health.' health!<br>';

while($h1->getIsDead() == false && $h2->getIsDead() == false){    
    $h1->attack($h2);  
    $h2->attack($h1);
}

实际结果

is starting with 317 health!
Superman is starting with 300 health!
attacks !

预期结果

Batman is starting with 317 health!
Superman is starting with 300 health!
Batman attacks Superman!

3 个答案:

答案 0 :(得分:2)

您的父类的构造函数如下

public function __construct($name) {
    $this->name;
    $this->isDead = false;
}

$this->name;一事无成,它试图获取该值。您需要通过

为它分配一个值
$this->name = $name;

然后,您的所有属性都是 private ,这意味着您无法通过执行$batman->name来访问它们。您将需要为此实现$batman->getName()(针对您要获取的每个属性)的吸气剂。如果它们是公开的,则可以通过执行$batman->name来获得它们,但是也可以用$batman->name = 'Robin';覆盖它们。最好使用吸气剂。

此外,determineHealth()应该应该是该类的方法并在$opponent对象上调用,而不是实际的函数。

class SuperHero {
    private $health;
    private $name;
    private $isDead;

    public function __construct($name, $health) {
        $this->name = $name;
        $this->health = $health;
        $this->isDead = false;
    }

    public function attack($opponent) { 
        $dmg = mt_rand(1, 10);
        echo $this->getName().' attacks '.$opponent->getName().' with '.$dmg." damage. ";
        $opponentHealth = $opponent->determineHealth($dmg);
        echo $opponent->getName().' has '.$opponentHealth." health left!<br />\n";
    }

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

    public function getHealth() {
        return $this->health;
    }

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

    public function determineHealth($health) {
        $this->health -= $health;
        if ($this->health <= 0) {
            $this->health = 0;
            $this->isDead = true;
        }
        return $this->health;
    }
}

class Batman extends SuperHero {
    public function __construct() {
        $health = mt_rand(1, 1000);
        parent::__construct('Batman', $health);
    }
}

class Superman extends SuperHero {
    public function __construct() {
        $health = mt_rand(1, 1000);
        parent::__construct('Superman', $health);
    }
}

$h1 = new Batman;
$h2 = new Superman;


echo $h1->getName().' is starting with '.$h1->getHealth()." health!<br>\n";
echo $h2->getName().' is starting with '.$h2->getHealth()." health!<br>\n";

$h1->attack($h2);  
$h2->attack($h1);

输出:

Batman is starting with 445 health!<br>
Superman is starting with 229 health!<br>
Batman attacks Superman with 5 damage. Superman has 224 health left!<br />
Superman attacks Batman with 9 damage. Batman has 436 health left!<br />

答案 1 :(得分:1)

超类的var_dump(); die;name成员是私有的,因此在子类中不可用。

您应该将它们更改为公开,或者应该创建公共获取器和设置器并改为使用它们。

答案 2 :(得分:0)

  

不能继承私有变量。

    <?php
class SuperHero {
    private $health;
    private $name;
    private $isDead;

    public function __construct($name) {
        $this->name = $name;
        $this->isDead = false;
    }

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

    public function attack($opponent) { 
        echo $this->name.' attacks '.$opponent->name.'!<br>';
        $dmg = mt_rand(1, 10);
        $opponentHealth = determineHealth($opponent, $dmg);
        echo $opponent->name.' has '.$opponentHealth.' health left!<br>';
    }
}

class Batman extends SuperHero {

    public function __construct() {
        parent::__construct('Batman');
        $this->health = mt_rand(1, 1000);
    }

}
class Superman extends SuperHero {

    public function __construct() {
        parent::__construct('Superman');
        $this->health = mt_rand(1, 1000);
    }

}

$h1 = new Batman;
$h2 = new Superman;

echo $h1->name().' is starting with '.$h1->health.' health!<br>';
echo $h2->name().' is starting with '.$h2->health.' health!<br>';

while($h1->getIsDead == false && $h2->getIsDead == false){    
    $h1->attack($h2);  
    $h2->attack($h1);
}

?>
  

在父类函数名()中,将返回从 parent :: __ construct('Name')传递的名称。

输出

Batman is starting with 327 health!
Superman is starting with 842 health!
Batman attacks Superman!