2015-12-24 20 views
4

我一直在努力的代碼和什麼地方出了錯:http://ideone.com/cvLRLgC#基本OOP - 製作類的詞典與構造

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace ConsoleApplication1 
{ 
    public class Minion 
    { 
     public static int manaCost; 
     public static int attack; 
     public static int health; 
     public static string cardText; 
     public Minion(int mana, int atk, int h, string txt) 
     { 
      manaCost = mana; 
      attack = atk; 
      health = h; 
      cardText = txt; 
     } 
     public void displayStats(string name) 
     { 
      Console.WriteLine(name + "\nMana Cost: " + manaCost + "\nAttack: " + attack + "\nHealth: " + health + "\n" + cardText + "\n"); 
     } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<string> indexList = new List<string>(); 
      Dictionary<string, Minion> minionList = new Dictionary<string, Minion>(); 

      //buffer so I start at 1 and not 0 
      indexList.Add("MissingNo"); 

      //make a Wolfrider card 
      indexList.Add("Wolfrider"); 
      Minion Wolfrider = new Minion(3, 3, 1, "Charge"); 
      minionList.Add(indexList[1], Wolfrider); 

      //make a Goldshire Footman card 
      indexList.Add("Goldshire Footman"); 
      Minion GoldshireFootman = new Minion(1, 1, 2, "Taunt"); 
      minionList.Add(indexList[2], GoldshireFootman); 

      //look through all my cards 
      for (int i = 1; i < indexList.Count(); i++) 
       minionList[indexList[i]].displayStats(indexList[i]); 
      Console.ReadLine(); 
     } 
    } 
} 

我一直在努力自學C#但是這已經絆倒了我。我想創建一個接受字符串的Dictionary然後返回一個Minion(新類)。

Minion接受四個參數,所以我必須專門用一行代碼來製作一個新的Minion,然後再添加到Dictionary中。

但是,當我經歷所有我有的僕從時,出於某種原因,第一個將我退回其他僕從的屬性。

Wolfrider 
Mana Cost: 1 
Attack: 1 
Health: 2 
Taunt 

Goldshire Footman 
Mana Cost: 1 
Attack: 1 
Health: 2 
Taunt 

該列表工作正常,因爲名稱是正確的...但是Wolfrider具有Goldshire Footman的屬性。

有沒有更高效/最優化的方法來做到這一點?如果不是,我做錯了什麼?

回答

6

的主要問題是,你的成員是static

public static int manaCost 

所以基本上,最後一個值,你影響勝。將其轉化爲實例屬性

public int ManaCost { get; set; } 

然後擺脫indexList,並直接使用你的爪牙的名字作爲字典的關鍵。

+1

它的工作原理!非常感謝! –

1

那麼請刪除所有班級成員的關鍵字static。你不希望所有的奴才擁有相同的價值觀嗎?

您還可以添加字段或屬性name到類:

public class Minion 
    { 
     public readonly string name; 
     public int manaCost; 
     public int attack; 
     public int health; 
     public string cardText; 

     public Minion(string name, int mana, int atk, int h, string txt) 
     { 
      this.name = name; 
      this.manaCost = mana; 
      this.attack = atk; 
      this.health = h; 
      this.cardText = txt; 
     } 
     public void displayStats() 
     { 
      Console.WriteLine(name + "\nMana Cost: " + manaCost + "\nAttack: " + attack + "\nHealth: " + health + "\n" + cardText + "\n"); 
     } 
    } 

在你Main方法,你並不真的需要這個List<string>與你的字典工作。您可以將其刪除並將您的代碼更改爲:

 Dictionary<string, Minion> minionList = new Dictionary<string, Minion>(); 

     Minion Wolfrider = new Minion("Wolfrider", 3, 3, 1, "Charge"); 
     minionList.Add(Wolfrider.name , Wolfrider); 

     //make a Goldshire Footman card 
     Minion GoldshireFootman = new Minion("Goldshire", 1, 1, 2, "Taunt"); 
     minionList.Add(GoldshireFootman.name, GoldshireFootman); 

     foreach(Minion minion in minionList.Values) 
      minion.DisplayStats(); 

     Console.ReadLine(); 
+0

啊,foreach循環,謝謝!有沒有一種方法可以訪問每個元素的「關鍵」?如果我使用的是foreach,是否有辦法讓我獲取卡名(輸入的字符串以訪問該僕從) –

+0

使用以下內容: foreach(Minion min in minionList) min.displayStats(); 導致錯誤:錯誤無法將類型「System.Collections.Generic.KeyValuePair <字符串,ConsoleApplication1.Minion>」到「ConsoleApplication1.Minion」 對不起,我不知道如何格式化這個評論。 –

+0

@CiscoOrtega它實際上是'foreach(Minion minion in minionList.Values)'(我更新了我的答案,請檢查它) – Fabjan

0

您不應該有類的靜態成員。刪除下面的靜態內容。

public static int manaCost; 
public static int attack; 
public static int health; 
public static string cardText; 

這裏是你可能會瞄準一個稍微乾淨的版本:

using System; 
using System.Collections.Generic; 

    namespace ConsoleApplication3 
    { 
     public class Minion 
     { 
      public string name { get; set; } 
      public int manaCost { get; set; } 
      public int attack { get; set; } 
      public int health { get; set; } 
      public string cardText { get; set; } 

      public void displayStats() 
      { 
       Console.WriteLine(name + "\nMana Cost: " + manaCost + "\nAttack: " + attack + "\nHealth: " + health + "\n" + cardText + "\n"); 
      } 

      class Program 
      { 
       static void Main(string[] args) 
       { 
        var minionList = new List<Minion>(); 

        minionList.Add(new Minion() { name = "Wolfrider", attack = 3, cardText = "Charge", health = 3, manaCost = 3 }); 
        minionList.Add(new Minion() { name = "GoldShire Footman", attack = 1, cardText = "Taunt", health = 1, manaCost = 2 }); 

        //look through all my cards 
        foreach (var minion in minionList) 
         minion.displayStats(); 
        Console.ReadLine(); 
       } 
      } 
     } 
    }