2011-11-29 145 views
2

我正在查看Should I use multiple classes for game? - 羅伯特皮特的答案。PHP類,繼承,實現

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() 
    { 

    } 



} 

在「人」我在也許是爲了貫徹落實「武器」而不是小馬,然後在「狙擊手」初始化合適的武器級?

class Sniper extends Human 
{ 
public $weapon; 

    public function __construct($weapon) 
    { 
    $this->weapon = new $weapon; 
    } 


$this->weapon->fire(); 


} 

或類似的東西?我很困惑它是如何工作的。

EXAMPLE 1 

class A { 
public $class; 

__construct() { 
$this->class = new $class; 
} 

hello() { 
$this->class->hello(); 
} 
} 

class B { 
public function hello() { 
echo 'hi'; 
} 
} 
+0

類人應該'extend',不'implement'類柯爾特。因爲它不是接口 – kravemir

+1

這真的取決於你。如果您正在爲擁有自己的最佳實踐列表的公司工作,那麼您就會問及閱讀文檔。但既然是你的項目,就去做吧。最好的學習方式是犯錯誤。 – Frank

回答

5

這取決於你,你將如何設計你的課程。

如果我是一個遊戲程序員,我可能會使用類似的東西(這是一個工作的minigame!)

<?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); 

demo

+0

感謝一個很好的例子,但我仍然不確定 - 如果我不知道什麼「種族」(人類,巨魔等)是如何。但我需要從數據庫中拉出來,擴展到一個變量,不起作用:s 我想這個效果更好,像NPC(怪物,老鼠),而不是「用戶」本人 – John

+0

@John:編輯了一下。我認爲現在應該清楚 –

+0

真的非常感謝您的幫助!它的工作很好,但我想,我仍然在思考如何擴展更多的東西,例如,如果我有一個「汽車」類,我也想擴展它,但我想我不能擴展多到一個班級? – John

2

我會避免含有人類類實現什麼關係武器。實現定義了類必須定義哪些方法,但不涉及類的屬性。

對象接口允許您創建代碼指定哪個 方法的類必須實現,而不必定義這些 方法是如何處理的。 (PHP Documentation: Object Interfaces

Class Human {} 

Class Sniper extends Human{ 
public $weapon 
} 

Interface Weapons { 
    public function fire() 
} 

Class Colt implements Weapons {} 
Class Bazooka implements Weapons {} 

這樣一個狙擊手是人(但以$武器屬性),和柯爾特,火箭筒等全部實現武器接口(迫使他們定義火()函數。

+0

'類人類{} 類狙擊手擴展人類{ 公共$武器 }'我不喜歡這個。我認爲$武器是每個人的財產,因此應該在人類的階層 –

+0

這可能是,並且忽略了這樣的聲明的政治含義:),我正在處理實施,而不是財產繼承。如果所有的人類都擁有武器,那麼無論如何,都要在父類中宣佈它。 – nageeb

5

在我身邊,我反而會使用這些類:

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 {} 

讓我現在來解釋:

iWeapon

描述的武器,不同的吸氣劑,setter和諸如DoDamage()或GetRandomAttackDamage()幾個動作。如你所願。這個界面可以讓你定義武器的基礎知識。你可以將它應用到Weapon類或派生它的任何其他類,但是我會保留它至少一個基本類。界面的目標是迫使所有武器添加相同的行爲,它具有攻擊傷害,射程,可能收費? (彈藥)

iAttacker/iAttackable

介紹一些可以攻擊或者被攻擊。這些接口通常會提供決定攻擊能力(被震驚,被動,被禁用?)還是被攻擊(不可見,無敵?)的功能,並提供攻擊其他實現的功能。人類或任何其他生物會執行這些說,嘿,我可以攻擊和攻擊。這個界面還會提供獲取者和制定者來控制這些生物攜帶的武器,或許是裝甲以防止對他們造成傷害?

關於接口

永遠記住那類代表相對具體的東西。如果您將您的課程命名爲「攻擊者」或「目標」,您可能會錯過課程的重點。代表有形事物的類和對象。另一方面,接口表示類可能採取的動作或角色。它們只指定了可以執行的操作,不提供執行本身,它們告訴該對象將執行的內容,從而聲明它們可以執行的操作。

希望這有助於:)


關於正確實施此屬性是什麼,我建議:

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()); 
     } 
    } 

} 
+0

TL; DR有點,但看起來像一個很好的答案。 +1 –

+0

什麼是「遠程武器」假定包含? =) – John

+0

遠程武器可以執行的基本實現,例如界面中描述的內容。然後每個其他武器可以但不應該延長遠程武器。在這一點上它的味道的問題,你想只有「武器」,「遠程武器/近戰武器」?它是一個非常大的話題:) –