2016-10-07 52 views
0

這是我完成作業的副本,所以對於挪威的一些評論道歉。簡單的方法用一種方法將3種不同類型的對象添加到一個ArrayList中?

我的問題是:
我有一個項目ArrayList,它包含類Item,Swords,Potions的對象。 (藥水&劍是物品的子類)。我有3種不同的方法將這些對象添加到ArrayList。

我的問題其實很簡單。有沒有一種(簡單的,我是新的和學習仍然)的方式,我沒有3種不同的方法看起來都一樣,所以將所有Item和Item的子類添加到ArrayList。 (addItem,addSword,addPotion),都接近代碼的底部。

我想是這樣的(通過沒有想過,只是寫的東西)

public void addItem(String typeofItem){ 
    item = new typeofItem; 
    items.add(item); 
} 

有很多更多的代碼,但我覺得這是什麼有關。

public class Player 
{ 
    // Fields ------- 
    private String name; 
    private String type; 
    private int health; 
    private ArrayList<Item> items = new ArrayList<>(); 
    private Item item; 
    private Swords sword; 
    private Potions potion; 
    private Checker valid; 
    private int maxCarry; 
    private int gold; 
    private int currentWeight; 
    // ---------------- 

    /** 
    * Klassens konstruktør. Players health vil alltid settes til 100. 
    * 
    * @param name   Player navn 
    * @param type   Player type 
    * @param goldValue Players starting gold 
    * @param setMaxWeight Players max carry weight 
    */ 
    public Player (String name, String type, int goldValue, int setMaxWeight) { 
     valid = new Checker();      // Creates a checker object to do String and integer checks 

     this.name = valid.checkString(name);   // Setter player name, etter å ha sjekka at den ikke er tom 
     this.type = checkType(type);     // Setter type, etter å ha processet den 
     health = 100;        // Health skal alltid starte som 100 
     maxCarry = valid.checkInt(setMaxWeight); // Sets max carry weight 
     currentWeight = 0;       // Start vekten til player 
     gold = valid.checkInt(goldValue);   // setter goldbeholding etter å ha sjekka at den ikke er negativ 
    } 

    /** 
    * En metode for å selge ett Item objekt, reduserer gold og øker weight ved kjøp. 
    * 
    * @param item  Item object to sell 
    */ 
    private void buyItem(Item item) 
    { 
     // Temporary values given from items methods getweight and getvalue to be used in a mutation 
     int tempWeight; 
     int tempValue; 
     tempWeight = item.getWeight(); 
     tempValue = item.getValue(); 
     // Checks if the item meets te conditions to be bought (enough gold, and can be carried) 
     if (checkConditions(tempValue, tempWeight)) { 
      // Adds the item to ArrayList if conditions met and updates gold and weight 
      items.add(item); 
      currentWeight += tempWeight; 
      gold -= tempValue; 
     } 
    } 

    /** 
    * Method to calculate if given value and weight is accepted to perform a purchase (total gold > 0 && total weight < maxWeight) 
    * 
    * @param value  Gold value of item 
    * @param weight Weight of item 
    * @return boolean Returns true if conditions met 
    */ 
    private boolean checkConditions(int value, int weight) 
    { 
     if (!(gold >= value)) 
     { 
      System.out.println("You don't have enough gold!"); 
      return false; 
     } else { 
      if (!(weight + currentWeight <= maxCarry)){ 
       System.out.println("You'll be too heavy carry this item!"); 
       return false; 
      } else { 
       // All conditions met 
       return true; 
      } 
     } 
    } 

    /** 
    * Adds an item to player inventory (uses method buyItem to process the item as a purchase) 
    * 
    * @param itemName String to name item 
    * @param itemDesc String to describe item 
    * @param itemValue int to declare items value 
    * @param itemWeight int to declare items Weight 
    * @param itemAction String to give item an action 
    */ 
    public void addItem(String itemName, String itemDesc, int itemValue, int itemWeight, String itemAction) 
    { 
     // A Player generated item, which needs all parameters to specify it 
     item = new Item(itemName, itemDesc, itemValue, itemWeight, itemAction); 
     buyItem(item); 
    } 

    /** 
    * Randomly generates a Sword object and adds it to players inventory (uses buyItem to process) 
    * @see Swords#Swords 
    */ 
    public void addSword() 
    { 
     sword = new Swords(); 
     buyItem(sword); 
    } 

    /** 
    * Randomly generates a Potion object and adds it to players inventory (uses buyItem to process) 
    * @see Potions#Potions 
    */ 
    public void addPotion() 
    { 
     potion = new Potions(); 
     buyItem(potion); 
    } 

回答

1

您可以創建一個名爲Factory類的東西。工廠類的職責是爲您生成一種物品的實例。

https://www.tutorialspoint.com/design_pattern/factory_pattern.htm

基本上你換你的一類說說方法並返回該對象的新實例。

public class ItemFactory{ 

    public static Item addItem(String typeofItem){ 
     switch(typeofItem){ 
      case "Sword": 
       return new Sword(); 
      case "Potion": 
       return new Potion(); 
      ... 
      default: 
       //put any code here that is like the "else" of an if-else block 
       return null; 
     } 
    } 

} 

然後,當你需要添加特定類型的新項目:

buyItem(ItemFactory.addItem("Sword")); 
+0

這會創建實例化項目的新對象?我希望劍/魔藥/物品在後期可以在玩家和商店之間移動。並有每個的多個實例。如在,不,我需要一把劍=新劍();因爲它是一個獨立的對象 – Peebl

+0

它們存儲在項目數組列表中。如果您需要訪問它們,您可以從此列表中進行訪問。你需要使用劍變量的唯一原因是如果你需要跟蹤一個非常特殊的劍與所有可用的劍。例如。如果你需要跟蹤當前/玩家的劍。但是,如果你有兩把不同的劍,那麼每次你調用addSword時,它都會覆蓋劍中變量與新的一樣,所以你不得不從數組列表中訪問它。 – Brion

+0

我加了這個,但是我還不太熟悉交換機。如果沒有滿足條件,我會返回什麼?我如何寫?類似的,返回新的藥水();}其他{返回null;}? – Peebl

相關問題