2014-12-03 19 views
0
class Rifle { 
    void shoot() { 
     System.out.println("rifle shoot"); 
    } 

    void reload() { 
     System.out.println("rifle reload"); 
    } 

    void onlyRifle() { 
     System.out.println("rifle!"); 
    } 
} 

class Shotgun { 

    void shoot() { 
     System.out.println("shotgun shoot"); 
    } 

    void reload() { 
     System.out.println("shotgun reload"); 
    } 

    void onlyShotgun() { 
     System.out.println("shotgun!"); 
    } 
} 

我需要類武器,這將是使用方法拍攝()從步槍和方法從Shotgun重裝() 我怎樣才能使用接口? 想象一下,那我不能修改類步槍和獵槍類如何創建兩個類的接口並標記可以使用的方法?

我的壞實現是

class Weapon { 
    Rifle myRifle; 
    Shotgun myShotgun; 

    Weapon() { 
     myRifle = new Rifle(); 
     myShotgun = new Shotgun(); 
    } 

    void shoot() { 
     myRifle.shoot(); 
    } 

    void reload() { 
     myShotgun.reload(); 
    } 

    void onlyShotgun() { 
     myShotgun.onlyShotgun(); 
    } 

    void onlyRifle() { 
     myRifle.onlyRifle(); 
    } 
} 

...main(){ 
    Weapon myWeapon = new Weapon(); 
    myWeapon.shoot(); 
    myWeapon.reload(); 
    myWeapon.onlyRifle(); 
    myWeapon.onlyShotgun(); 
} 

的工作,我想,但我怎麼能做到這一點更簡單地使用接口?

+0

如果你不能改變'Rifle'和Shotgun',我看不出接口如何幫助你,因爲你不能改變這些類實現界面。 – Eran 2014-12-03 09:11:55

+0

如果你說你不能修改這些類,你是否仍然可以在它們中寫下這麼多 ​​- 實現武器 – rajesh 2014-12-03 09:11:55

+0

是否需要創建一個武器對象?或者你可以創建任何對象? – Secondo 2014-12-03 09:17:36

回答

1

Weapon應該是一個接口

interface Weapon 
{ 
    void shoot(); 
    void reload(); 
} 

然後在你的混凝土情況下,你實現該接口:

class Rifle implements Weapon 
{ 
    /*ToDo - implement shoot and reload*/ 

    void onlyRifle() { 
     /*ToDo - specific code here*/ 
    } 
} 

然後說,如果你有r這是Rifle的實例,您可以添加r t o Weapon的集合。這是不可能的,你永遠需要onlyRifleRifle - 具體的東西只需要通過建設。

+0

現在,它將無限期地遞歸。當你在'Rifle'的實例上調用'.onlyRifle()'時,它會在它存儲的實例'myRifle'上調用它,並且會在它存儲的實例上調用它。 – 2014-12-03 09:20:10

+0

糟糕。刪除了功能體。原則依然成立。 – Bathsheba 2014-12-03 09:21:36

+0

是的,原則是正確的,這就是爲什麼我沒有downvote :) – 2014-12-03 09:22:13

0

一種方法是有接口武器定義,並有2個新的類,讓我們稱之爲MyRifle延伸步槍和實施武器和MySHotgun延伸獵槍和實現武器

要求您擴展現有的類,因爲你何況,你不能修改現有的

所以

interface Weapon 
{ 
    void shoot(); 
    void reload(); 
} 

而且

class MyRifle extends Rifle implements Weapon{ 
// actual implementations of your methods 
} 
1

嘗試同樣喜歡你使用的組合物及使用接口,如:

interface Weapon { 
void shoot(); 
void reload(); 
} 

class ModifiedRifle implements Weapon { 
    Rifle myRifle; 
    Shotgun myShotgun; 
    public ModifiedRifle (Rifle rifle, Shotgun shotgun) { 
      this.myRifle = rifle(); 
      this.myShotgun = shotgun; 
    } 

    public void shoot() { 
     myRifle.shoot(); 
    } 

    public void reload() { 
     myShotgun.reload(); 
    } 
} 
0

你有一個超類武器,這需要一個WeaponType。你的武器類型是一個具有拍攝和重新加載方法的界面。

public class Weapon { 

WeaponType weaponType; 

    public Weapon(WeaponType weaponType) { 

     this.weaponType = weaponType; 
    } 

    public void shoot() { 

     this.weaponType.shoot(); 
    } 
    public void reload() { 

     this.weaponType.reload(); 
    } 
} 

你的接口:

public interface WeaponType { 

    public void shoot(); 
    public void reload(); 
} 

你的獵槍類:

public class Shotgun implements WeaponType { 

    @Override 
    public void shoot() { 

     System.out.println("Shotgun shoot!"); 
    } 

    @Override 
    public void reload() { 

     System.out.println("Shotgun reload!"); 
    } 
} 

用法示例:

Weapon myShotgun = new Weapon(new Shotgun()); 
myShotgun.shoot(); 
myShotgun.reload(); 
相關問題