如何解决什么是用于获取2个不同枚举的排列的最佳集合类型?
我正在寻找合适的集合类型以在C#中实现此结果。我想从我预先指定的几个枚举字段中创建一个项目列表。
- itemType
- itemMaterial
对于我添加的每个itemType,我都希望创建一个遵循此常规模式的新项目集(为方便概念传达,从Runescape中删除,不要紧追JaGex)
ItemType enum:
Helmet,Platebody,Platelegs,Boots
ItemMaterial enum:
Bronze,Iron,Steel,Black,White,Mithril,Adamant,Rune,Dragon...
Items List(?):
BronzeHelmet,IronHelmet,SteelHelmet,BlackHelmet,WhiteHelmet,MithrilHelmet,AdamantHelmet,RuneHelmet,DragonHelmet
BronzePlatebody,IronPlatebody,SteelPlatebody,BlackPlatebody,WhitePlatebody,MithrilPlatebody,AdamantPlatebody,RunePlatebody,DragonPlatebody
BronzePlatelegs,IronPlatelegs,SteelPlatelegs,BlackPlatelegs,WhitePlatelegs,MithrilPlatelegs,AdamantPlatelegs,RunePlatelegs,DragonPlatelegs
BronzeBoots,IronBoots,SteelBoots,BlackBoots,WhiteBoots,MithrilBoots,AdamantBoots,RuneBoots,DragonBoots
如果有人知道最好的数据类型来实现类似的目标,或者有合适的方法允许您迭代两者的组合,那么我将非常感激。
enum ItemType
{
FullHelmet,MediumHelmet,Chainbody,Plateskirt,Boots,Shoes,KiteShield,SquareShield,Battleaxe,WarHammer,Longsword,Shortsword,Dagger,Longbow,Shortbow,Crossbow,LongStaff,Staff,Wand
}
enum ItemQuality
{
Wood,Bronze,Dragon,Barrows,GodWars,Elite
}
解决方法
不需要特定的集合。只需声明item类并创建所有排列即可。
如果您有特定的要求,例如快速访问,快速迭代,一个指向另一个的项目等等,则需要选择一个集合。
public class Item {
public ItemType Type { get; set; }
public ItemQuality Material { get; set; }
public Item(ItemType type,ItemQuality material) {
this.Type = type;
this.Material = material;
}
}
由于这是一个枚举,并且不能动态创建,因此在启动时创建所有可能的组合:
List<Item> Items;
var types = Enum.GetValues(typeof(ItemType));
var materials = Enum.GetValues(typeof(ItemQuality));
foreach(ItemType type in types) {
foreach(ItemQuality material in materials) {
Items.Add(new Item(type,material));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。