-2

如果有一个使用 PHP、OOP 和 Extends 的非常简化的示例,我将非常感激。

创建角色类别、两种特定类型的法师和战士以及战斗系统的示例。

就像是:

class Character{

}
class Wizard extends Character{

}
class Warrior extends  Character{

}
class Battle {

}

只是为了理解逻辑。

4

2 回答 2

4

这只是一个开始,对于您的案例来说是一个糟糕的例子。

每个角色都有一个名字。因此,字符的名称可以在 Character 类中。

class Character {
    public $name = "unknown";
}

每个人也有标准的力量和生命

class Character {
    public $name = "unknown";
    public $force = 10;
    public $life = 100;
}

扩展类继承了这些属性。例如:

$me = new Wizard();
echo "The name of wizard is " . $me->name; // The name of wizard is unknown

我们可以在 costructor 方法中设置字符的名称

class Character {
    public $name = "unknown";
    public $force = 10;
    public function __construct($name) {
        $this->name = $name;
    }
}


$me = new Wizard("Gandalf");
echo "The name of wizard is " . $me->name; // The name of wizard is Gandalf

巫师拥有简单角色所没有的东西。以法力为例:

class Wizard extends Character{
    public $mana = 100;
}

$me = new Wizard("Gandalf");
echo "The name of wizard is " . $me->name; // The name of wizard is Gandalf
echo "\nAnd its mana is " . $me->mana;

战士拥有标准力量的倍数,因为他有武器。

class Warrior extends  Character{
    public $weapon = 12;
}

$opponent = new Warrior("Grunt");
echo "The name of warrior is " . $opponent->name . " and the powrfull of its weapon is " . $opponent->weapon;

每个角色都有一个强大的取决于它的力量:

class Character {
    public $name = "unknown";
    public $force = 10;
    public $life = 100;
    public function __construct($name) {
        $this->name = $name;
    }
    protected function get_powerfull() {
        return $this->force;
    }
}

但对于巫师来说,力量取决于法力

class Wizard extends Character{
    public $mana = 100;
    // override
    public function get_powerfull() { 
        return $this->mana;
    }
}

对于战士来说,取决于武器及其力量。

class Warrior extends  Character{
    public $weapon = 12;
    // override
    public function get_powerfull() {
        return $this->weapon*$this->force;
    }
}

每个角色都可以战斗,结果取决于取决于等级的强大

class Battle {
    public function fight(Character $player1, Character $player2) {
        $damage1 = $player1->get_powerfull();
        $damage2 = $player2->get_powerfull();
        $player1->life -= $damage2;
        $player2->life -= $damage1;

        echo "{$player1->name} hits {$player2->name} for $damage1 points.<br/>";
        echo "{$player2->name} hits {$player1->name} for $damage2 points.<br/>";
    }
}

$me = new Wizard("Gandalf");
echo "The name of wizard is " . $me->name . "<br/>"; // The name of wizard is Gandalf

echo "\nAnd its mana is " . $me->mana. "<br/>";

$opponent = new Warrior("Grunt");
echo "The name of warrior is " . $opponent->name . " and the powrfull of its weapon is " . $opponent->weapon. "<br/>";

所以:

$battle = new Battle();
$battle->fight($me,$opponent);

输出:

Gandalf hits Grunt for 100 points.
Grunt hits Gandalf for 120 points.

我希望这有帮助

于 2013-08-27T22:52:19.673 回答
3

This example that you posted, simplified:

class Character{

}

class Mage extends Character{

}

class Warrior extends Character{

}

class Battle {

}

I've removed the empty comments, the extra space after Warrior extends and corrected the spelling of Mage.

于 2013-08-27T21:55:37.183 回答