简体   繁体   English

如何获取数组中每个 object 的属性?

[英]How do I get properties for each object in the array?

I'm trying to write a card game and I have this question.我正在尝试写一个纸牌游戏,我有这个问题。

I have a List<int> address 1 Player;我有一个List<int> address 1 Player; which contains a set of cards with parameters.其中包含一组带参数的卡片。

I need to pull the parameter of each card from this sheet.我需要从这张表中提取每张卡的参数。

For example, List<int> address1Player contains cards with id 1, 2, 3. I need to find out what colors of cards are in List<int> adress1Player .例如, List<int> address1Player包含 id 为 1、2、3 的卡片。我需要找出List<int> adress1Player中卡片的 colors 是什么。 Colors are stored in int . Colors 存储在int中。

The color getting parameter looks like this颜色获取参数如下所示

public int PropertyColor(int adress){
    return  allProperties [adress].GetComponent<Plot> ().GetColor ();
}

How do I make sure that I end up with an array with the colors of each card?如何确保我最终得到一个包含每张卡的 colors 的阵列?

A List<int> only contains a list of integers - in your case, IDs. List<int>仅包含整数列表-在您的情况下为ID。 You want to store a data structure of colors (and ostensibly, some other values about the cards), so a List is not the collection you want.您想存储 colors 的数据结构(表面上,还有一些关于卡片的其他值),因此List不是您想要的集合。 First, let's think about the data structure that our collection will hold, then we'll come back to the collection.首先,让我们考虑一下我们的集合将持有的数据结构,然后我们将回到集合。

Our card in our game has at least two properties: ID and an integer based Color.我们游戏中的卡片至少有两个属性:ID 和基于 integer 的颜色。 In C#, we write classes or structs to group up logical bundles of properties into an object.在 C# 中,我们编写类或结构来将属性的逻辑包分组到 object 中。 It would look (pretty simply) like this:它看起来(很简单)是这样的:

public struct KorsunsCard
{
   public int Id;
   public int CardColor
}

Now, we have a "card" object that has properties that we can check and set like so:现在,我们有一个“卡片”object,它具有我们可以检查和设置的属性,如下所示:

KorsunsCard greenCard = new KorsunsCard() { Id = 1, CardColor = 6 };
greenCard.CardColor = 5; // change the color to "5"
if (greenCard.Id == 2) { .. do some stuff }

Then, we can have methods just return the entire card:然后,我们可以让方法返回整张卡片:

public KorsunsCard GetCardWithID(int Id) 
{
    KorsunsCard returnCard = ...
    ... we'll get to this part in a moment ...
    return returnCard;
}

Now, about those collections.现在,关于那些 collections。 Selecting a data structure to use is the heart of C#.选择要使用的数据结构是 C# 的核心。 Collections are "groups" of objects - in our case, KorsunsCard s. Collections 是对象的“组”——在我们的例子中是KorsunsCard s。 Each collection can do different things well - List s can get access to a card by "index" (not Id), iterate over the whole list, sort themselves, etc. Dictionary s are meant for looking up a card by a key, and while they can iterate over the whole dictionary, they aren't meant for that typically so the syntax is a little more involved.每个集合都可以做好不同的事情 - List可以通过“索引”(而不是 Id)访问卡片,遍历整个列表,对自己进行排序等。 Dictionary用于通过键查找卡片,并且虽然它们可以遍历整个字典,但它们通常并不适用于此,因此语法涉及更多。 Not difficult, just not as easy as a List .不难,只是不如List容易。 You also might want a HashSet , a collection that can only have one unique item - but isn't sorted, like a dictionary.您可能还需要一个HashSet ,一个只能有一个唯一项目的集合 - 但没有排序,就像字典一样。

I can't suggest the best solution, because it depends on your game's rules (will your deck always have the same number of cards? one and only of each kind of card? does a user build their own deck from a pool of available cards?).我不能建议最好的解决方案,因为这取决于你的游戏规则(你的牌组总是有相同数量的牌吗?每种牌只有一张?用户是否从可用牌池中构建自己的牌组? ?)。

Let's start with some cards:让我们从一些卡片开始:

KorsunsCard ace = new KorsunsCard() { Id = 1, Color = 1 };
KorsunsCard deuce = new KorsunsCard() { Id = 2, Color = 2 };
KorsunsCard trey = new KorsunsCard() { Id = 3, Color = 3 };

If you wanted a List , you could declare it and add some values to it like so:如果你想要一个List ,你可以声明它并向它添加一些值,如下所示:

List<KorsunsCard> myDeck = new List<KorsunsCard>();
myDeck.Add(ace);
myDeck.Add(deuce);
myDeck.Add(trey)

int deuceColor = deuce.Color; // deuce's color
return myDeck[0]; // ace, but be aware the list can be shuffled/sorted!

foreach (KorsunsCard kc in myDeck) // iterate on the whole deck
{
    kc.Color = 4; // set the entire decks color to 4 , one at a time
}

The generic collection types Dictionary , HashSet , Queue , Stack may all be relevant for your game, depending on how you typically interact with the deck and the game rules.通用集合类型DictionaryHashSetQueueStack可能都与您的游戏相关,具体取决于您通常如何与套牌和游戏规则进行交互。 Hopefully I've given you enough from List that you can go and read up on these other collection types and put them to use.希望我已经从List中为您提供了足够的信息,您可以 go 并阅读这些其他集合类型并使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中,如何获取数组内部对象的属性? - In C# how do I get the properties of an object that is inside an array? 如何使用for循环将带编号的对象属性设置为数组中的值? - How do I set numbered object properties to values in an array with a for loop? 如何使用反射获取对象的属性? - How do I get the properties of an object using reflection? 如何在此JSON字符串的数组中获取属性? - How do I get properties in the array in this JSON string? 使用C#,如何获取包含Windows文件夹或文件的所有可用属性的对象(或kv对数组)? - Using C#, how do I get an object (or array of kv pairs) containing all available properties of a Windows folder or file? 如何在C#的锯齿状数组中引用对象数组的属性? - How do I refer to properties of object array inside jagged array in c#? 如何在PropertyGrid中查看对象属性? - How do I view object properties in PropertyGrid? 如何从二维数组中的每个数组中获取最大值并将其分配给新数组 - How do I get the largest values from each array within a 2D array and assign it to a new array 如果在C#中的字符串中有类型名称,如何获取对象的公共属性? - How do I get the public properties of an Object if I have the type name in a string in C#? 如何使导航属性正常工作? - How do I get Navigation Properties working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM