【问题标题】:C# Basic OOP - Making a Dictionary of a Class with ConstructorsC# Basic OOP - 使用构造函数制作类的字典
【发布时间】:2016-03-30 19:48:58
【问题描述】:

我一直在尝试的代码以及出了什么问题:http://ideone.com/cvLRLg

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#,但这一直困扰着我。我想制作一个接受字符串然后返回 Minion(新类)的字典。

一个 Minion 在创建时接受四个参数,因此在将其添加到字典之前,我必须专门编写一行代码来创建一个新的 Minion。

但是,当我检查我拥有的所有小兵时,出于某种原因,第一个小兵将其他小兵的属性还给我。

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

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

名单正常工作,因为名字是正确的......但狼骑兵具有金郡步兵的属性。

有没有更有效/优化的方法来做到这一点?如果没有,我做错了什么?

【问题讨论】:

    标签: c# list class oop dictionary


    【解决方案1】:

    主要问题是你的成员是static

    public static int manaCost
    

    所以基本上,你影响的最后一个值获胜。将它们转换为实例属性

    public int ManaCost { get; set; }
    

    然后去掉indexList,直接用你的Minion的名字作为字典键。

    【讨论】:

      【解决方案2】:

      从所有班级成员中删除关键字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&lt;string&gt; 来处理您的字典。您可以将其删除并将代码更改为:

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

      【讨论】:

      • 啊,foreach 循环,谢谢!有没有办法可以访问每个元素的“键”?如果我使用 foreach,有没有办法让我获得卡名(输入的字符串以访问该仆从)
      • 使用如下:foreach(Minion min in minionList) min.displayStats();导致错误:错误 1 ​​无法将类型 'System.Collections.Generic.KeyValuePair' 转换为 'ConsoleApplication1.Minion' 对不起,我不知道如何格式化此评论。
      • @CiscoOrtega 实际上是foreach(Minion minion in minionList.Values)(我更新了我的答案,请检查)
      【解决方案3】:

      你的类不应该有静态成员。去除下面的静电。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-12
        • 1970-01-01
        • 2014-11-21
        相关资源
        最近更新 更多