C#Basic OOP-使用构造函数制作类的字典
我一直在尝试的代码以及出错的原因: 列表工作正常,因为名称正确。。。但是狼骑士拥有戈德郡步兵的属性C#Basic OOP-使用构造函数制作类的字典,c#,list,class,oop,dictionary,C#,List,Class,Oop,Dictionary,我一直在尝试的代码以及出错的原因: 列表工作正常,因为名称正确。。。但是狼骑士拥有戈德郡步兵的属性 有没有更有效/优化的方法来实现这一点?如果没有,我做错了什么?主要问题是您的成员: 所以基本上,你影响的最后一个值是赢的。将它们转换为实例属性: 然后去掉索引列表,直接用你的仆从的名字作为字典键。从你所有的类成员中删除关键字静态。你不希望所有的仆从都有相同的价值观,不是吗 您还可以向类中添加字段或属性name: public class Minion { public
有没有更有效/优化的方法来实现这一点?如果没有,我做错了什么?主要问题是您的成员: 所以基本上,你影响的最后一个值是赢的。将它们转换为实例属性:
然后去掉
索引列表
,直接用你的仆从的名字作为字典键。从你所有的类成员中删除关键字静态
。你不希望所有的仆从都有相同的价值观,不是吗
您还可以向类中添加字段或属性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
方法中,你并不需要这个列表来使用字典。您可以将其删除并将代码更改为:
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();
Dictionary minionList=newdictionary();
仆从狼骑士=新仆从(“狼骑士”,3,3,1,“冲锋”);
minionList.Add(Wolfrider.name,Wolfrider);
//制作一张Goldshire Footman卡
Minion GoldshireFootman=新的仆从(“Goldshire”,1,1,2,“嘲弄”);
minionList.Add(GoldshireFootman.name,GoldshireFootman);
foreach(minionList.Values中的Minion Minion)
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();
}
}
}
}
使用系统;
使用System.Collections.Generic;
命名空间控制台应用程序3
{
公务舱仆从
{
公共字符串名称{get;set;}
公共成本{get;set;}
公共整数攻击{get;set;}
公共int运行状况{get;set;}
公共字符串cardText{get;set;}
公共void displaysts()
{
Console.WriteLine(名称+“\n名称成本:“+manaCost+”\n地址:“+attack+”\n高度:“+health+”\n“+cardText+”\n”);
}
班级计划
{
静态void Main(字符串[]参数)
{
var minionList=新列表();
添加(新的仆从(){name=“Wolfrider”,攻击=3,cardText=“Charge”,健康=3,魔法消耗=3});
添加(新仆从(){name=“GoldShire Footman”,攻击=1,cardText=“taint”,健康=1,魔法消耗=2});
//看看我所有的卡片
foreach(minionList中的var minion)
minion.displayStats();
Console.ReadLine();
}
}
}
}
啊,foreach循环,谢谢!是否有一种方法可以访问每个元素的“键”?如果我使用foreach,有没有办法通过以下方式获取卡名(输入以访问该仆从的字符串):foreach(仆从列表中的仆从min)min.displayStats();导致错误:错误1无法将类型“System.Collections.Generic.KeyValuePair”转换为“ConsoleApplication1.Minion”,很抱歉,我不知道如何设置此注释的格式。@CiscoOrtega它实际上是foreach(minionList.Values中的Minion Minion Minion)
(我更新了答案,请检查)
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");
}
}
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();
}
}
}
}