C#Basic OOP-用构造函数生成类的字典



我一直在尝试的代码以及出现的问题: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(新类)的Dictionary。

Minion在制作时接受四个参数,所以在将其添加到字典之前,我必须用一行代码来制作一个新的Minion。

然而,当我浏览我拥有的所有小黄人时,出于某种原因,第一个是把其他迷你人的财产还给我。

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

列表工作正常,因为名称正确。。。但是Wolfrider拥有Goldshire Footman的特性。

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

主要问题是您的成员是static:

public static int manaCost

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

public int ManaCost { get; set; }

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

从所有类成员中删除关键字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();

您的类不应该有静态成员。清除下面中的静电。

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

最新更新