PHP classes, inheriting, implements
我在看我是否应该使用多个类进行游戏?-罗伯特·皮特的回答。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | interface Weapons { public function Fire(); } class Colt implements Weapons { function Fire() { echo 'Fire!'; } } abstract class Human implements Colt { } class Sniper extends Human { public function __construct() { } } |
关于"人",我是不是先执行"武器",再执行"小马",然后在"狙击手"初始化正确的武器类?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Sniper extends Human { public $weapon; public function __construct($weapon) { $this->weapon = new $weapon; } $this->weapon->fire(); } |
或者类似的?我对它是如何工作感到困惑。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | EXAMPLE 1 class A { public $class; __construct() { $this->class = new $class; } hello() { $this->class->hello(); } } class B { public function hello() { echo 'hi'; } } |
在我这边,我会使用这些类:
1 2 3 4 5 6 7 8 9 10 | Interface iWeapon {} Interface iAttacker {} Interface iAttackable {} Class Human Implements iAttacker, iAttackable {} Class RangedWeapon Implements iWeapon {} Class Colt extends RangedWeapon {} Class M4AA extends RangedWeapon {} Class Sniper extends RangedWeapon {} Class Shotgun extends RangedWeapon {} |
现在我来解释一下:
我的武器
描述武器、不同的getter、setter和一些操作,如dodamage()或getrandomatackdamage()。就如你所愿。这个界面允许你定义武器的基本要素。你可以把它应用到武器类或任何其他派生它的类中,但我会将它保留到至少一个基本类中。接口的目标是强制所有武器添加相同的行为,它有攻击伤害,射程,也许收费?(弹药)
IAttacker/IAttackable公司
描述可以攻击或被攻击的东西。这些接口通常会提供确定攻击能力的功能(是否被击昏、固定、禁用?)或者被攻击(看不见,战无不胜?)并提供攻击其他实现的功能。人类或任何其他生物都会执行这些命令,说,嘿,我可以攻击和被攻击。这个界面还将提供getter和setter来控制这些生物携带的武器,也许是护甲来防止对他们的伤害?
关于接口
记住,类总是代表一些相对具体的东西。如果将类命名为"攻击者"或"目标",则可能缺少类的点。表示某种有形事物的类和对象。另一方面,接口表示类可能采取的操作或角色。它们只指定可以做什么,本身不提供实现,它们告诉对象将实现什么,从而声明它们可以做什么。
希望这有帮助:)
关于正确实现属性,我建议:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | class Player implements iAttacker, iAttackable { protected $playerClass; protected $race; protected $weapon; protected $armor; public function setClass(playerClass $class){ $this->playerClass = $class; } public function getClass(){ return $this->playerClass; } public function setRace(race $race){ $this->race = $race; } public function getRace(){ return $this->race; } public function setWeapon(damagingGear $weapon){ $this->weapon = $weapon; } public function getWeapon(){ return $this->weapon; } public function setArmor(protectiveGear $armor){ $this->armor = $armor; } public function getArmor(){ return $this->armor; } public function attack(iAttackable $target){ //Check we can attack if(!$this->canAttack()){ //Check we are not immobilized or disabled throw new CannotAttackException($this->getNonAttackingReason()); }elseif(!$this->weapon->canFire()){ throw new CannotAttackException($this->weapon->getNonFireableReason()); } //We can attack, roll the damage if($target->isAttackable()){ $target->sufferDamage($this->weapon->rollDamage()+$this->race->getRangedDamageBonus()); }else{ thrown new CannotBeAttackedException($target->getNonAttackableReason()); } } } |
这取决于你,你将如何设计你的课程。
如果我想成为一个游戏程序员,我可能会使用类似的东西(这是一个有效的小游戏!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?php class Fight { public $weapon; public $name; public function __construct($weapon, $name) { $this->name = $name; $this->weapon = new $weapon; } public function fire($person) { $this->weapon->fire($person); } public function changeWeapon($weapon) { $this->weapon = new $weapon; } } class Sniper extends Fight { } class Terrorist extends Fight { } class MauserSniper { function fire($person) { echo"Firing $person->name! Miss! Your rifle is damaged<br />"; } } class AK47 { function fire($person) { echo"Firing $person->name! Hit with AK47!<br />"; } } class SteyrSSG { function fire($person) { echo"Firing $person->name! Hit!<br />"; } } $sniper1 = new Sniper("MauserSniper","Martin"); $sniper2 = new Sniper("SteyrSSG","Daniel"); $terrorist = new Terrorist("AK47","Mike"); $sniper1->fire($sniper2); //we'll miss this one. $sniper1->changeWeapon("SteyrSSG"); $sniper1->fire($sniper2); //hit! $terrorist->fire($sniper1); |
演示
我会避免让人类阶级实施任何与武器有关的事情。实现定义类必须定义哪些方法,但不处理类的属性。
Object interfaces allow you to create code which specifies which
methods a class must implement, without having to define how these
methods are handled.
(PHP Documentation: Object Interfaces)
1 2 3 4 5 6 7 8 9 10 11 12 | Class Human {} Class Sniper extends Human{ public $weapon } Interface Weapons { public function fire() } Class Colt implements Weapons {} Class Bazooka implements Weapons {} |
因此,狙击手是人(但拥有$武器属性),而Colt、Bazooka等都实现了武器接口(强制他们定义fire()函数)。