简体   繁体   English

在同一个类中创建一个类的列表?

[英]Creating a List of a class within the same class?

I'm practicing C# and have only been learning for about a month.我正在练习 C# 并且只学习了大约一个月。 I have a question that maybe is a bit on the beginner side so I hope someone doesn't mind answering for me.我有一个问题可能是初学者方面的问题,所以我希望有人不介意为我回答。

I have a class called yourCharacter.我有一个名为 yourCharacter 的类。 In this class sits all the information for someone's character.在这个类中包含有关某人角色的所有信息。 I want to give the user the ability to create a new character so I've created a method/function to do so.我想让用户能够创建一个新角色,所以我创建了一个方法/函数来这样做。 My question is, can I place a function within yourCharacter that creates a List of yourCharacter.我的问题是,我可以在 yourCharacter 中放置一个函数来创建 yourCharacter 列表吗? Is this doable?这是可行的吗? Is it bad form to do it this way?这样做是不好的形式吗? Should I be creating this list in my Main class and then calling a function within the main class to do this?我应该在我的 Main 类中创建这个列表,然后在主类中调用一个函数来做到这一点吗?

I hope my question is clear enough, please let me know if you need further detail.我希望我的问题足够清楚,如果您需要更多详细信息,请告诉我。 The only reason I want to do this is because in my head it makes more sense to group my methods/functions with the class it is manipulating and or working with.我想这样做的唯一原因是因为在我的脑海中,将我的方法/函数与它正在操作和/或使用的类进行分组更有意义。

class yourCharacter{

     //insert a bunch of variables here


     public static void newChar(){

          List newCharacter = new List<yourCharacter>();

     }
}

What you have there is doable for sure.你所拥有的肯定是可行的。

A class can contain itself as a member.一个类可以包含它自己作为一个成员。 This might make sense in some scenario, but think about yours and see whether it applies.这在某些情况下可能有意义,但请考虑您的情况,看看它是否适用。 In your case it seems as if you're trying to have a mechanism to group characters - but it's not a good way to do it.在您的情况下,您似乎正在尝试建立一种对字符进行分组的机制 - 但这不是一个好方法。 Why?为什么? Read on...继续阅读...

Say you had a box.假设你有一个盒子。 This box contained another box inside... that box could contain another box inside... So in that case, if I made a class which represents this kind of box it might make sense to add that class into itself.这个盒子里面包含另一个盒子……那个盒子里面可以包含另一个盒子……所以在这种情况下,如果我创建了一个代表这种盒子的类,那么将那个类添加到自身中可能是有意义的。 Making a class like that represents a system in reality where a box contains other boxes.制作这样的类代表现实中一个盒子包含其他盒子的系统。

What system are you trying to abstract, so that it is useful that a character contains other characters?你想抽象什么系统,以便一个字符包含其他字符是有用的? I can think of something: let's imagine a character in your game can swallow other characters and they'll live inside his tummy...我能想到一些事情:让我们想象一下你游戏中的一个角色可以吞下其他角色,而他们会住在他的肚子里......

If you want to group them - you'd likely want to use another class which represents the system under which your characters are grouped like this: Say your game has an adventure party!如果您想将它们分组 - 您可能需要使用另一个类来代表您的角色分组的系统,如下所示:假设您的游戏有一个冒险派对! The adventure party can invite other people to join and they go questing together.冒险派对可以邀请其他人加入,他们一起去寻找。 Let's say this adventure party has a leader which decides who can join and who can't.假设这个冒险派对有一个领导决定谁可以加入,谁不能。 You still probably wouldn't make a character which contains other characters... you would rather create a class called "AdventureGroup" with a list of "ordinary" party members and a leader, you would assign a string which represents the "group nickname", and all other kinds of things which represent the group as an entity.您仍然可能不会创建包含其他字符的字符......您宁愿创建一个名为“AdventureGroup”的类,其中包含“普通”党员和领导者的列表,您将分配一个表示“组昵称”的字符串”,以及所有其他将群体表示为一个实体的事物。 Something like that... you get the jist.类似的东西......你明白了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM