简体   繁体   English

使用类的相同对象,但成员不同

[英]Using same objects of the class, but with different members

Okay, so I want to use the same kind of object five times in a class. 好的,所以我想在一个类中使用五次相同的对象。 They all have the same attributes, but they're just present there multiple times. 它们都具有相同的属性,但是它们只是多次出现。

     for(int i=0;i<5;i++){
         army1.Add(monkey);
         army1.Add(flyingMonkey);
         army1.Add(wizard);
         army1.Add(balloon);
     }

The Troops class contain two attributes: Name and Health. Troops类包含两个属性:Name和Health。 I want all the monkeys to have different instances, but without making different variables like monkey1, monkey2, or without using an array. 我希望所有的猴子都具有不同的实例,但又不要做出不同的变量(例如monkey1,monkey2)或不使用数组。 All the monkeys are taking damage in different amounts, but right now, the damage is stacking up. 所有的猴子都受到不同程度的伤害,但是现在,伤害正在累积。

Code: https://dotnetfiddle.net/YRukEz 代码: https//dotnetfiddle.net/YRukEz

The cause for the different output is the way you populate your list of Troop objects: 输出不同的原因是填充Troop对象列表的方式:

 for(int i=0;i<5;i++){
     army1.Add(monkey);
     army1.Add(flyingMonkey);
     army1.Add(wizard);
     army1.Add(balloon);
 }

You are doing this in a loop with five iterations. 您将通过五个迭代循环执行此操作。 Therefore, each of your Troop instances will be added to your list five times (leaving you with an army1 list with 20 elements). 因此,您的每个Troop实例都会被添加到列表中五次 (留下带有20个元素的army1列表)。 Accordingly, each of your Troop elements has to be removed five times until they are not in the list anymore. 因此,每个Troop元素必须删除五次,直到不再在列表中。

Simply remove the loop, and the resulting number of lines from Code 2 will match that generated by Code 1: 只需删除循环,代码2中得到的行数将与代码1中生成的行数匹配:

 army1.Add(monkey);
 army1.Add(flyingMonkey);
 army1.Add(wizard);
 army1.Add(balloon);

With that said, adding units in a loop is not necessarily wrong. 话虽如此,在循环中添加单位不一定是错误的。 When reading your code, my first thought was that you were trying to create an army that contains several units of the same type (four monkeys, four flying monkeys, four wizards, and four balloons). 在阅读代码时,我首先想到的是,您正在尝试创建一支包含数个相同类型单位的军队(四只猴子,四只飞行的猴子,四个巫师和四个气球)。

You can do that, but be aware that the way you do this, your army will not contain four different monkeys, but four times the same monkey, because you are adding the same object instance four times. 您可以这样做,但是请注意,这样做的方式是,您的军队不会包含四只不同的猴子,而是四只相同的猴子,因为您要添加同一对象实例四次。 When you reduce the health of one of them, you will see that change reflected in the three other items of the same type. 当减少其中之一的运行状况时,您会看到该变化反映在同一类型的其他三个项目中。

To generate an army with several units of the same type, you'd have to create the units in each iteration instead by putting the respective initialization code into the loop, as well: 要生成具有多个相同类型单位的军队,您还必须在每次迭代中创建单位,而不是将相应的初始化代码放入循环中:

for(int i=0;i<5;i++){
    Troop monkey = new Troop();
    Troop flyingMonkey = new Troop();
    Troop wizard = new Troop();
    Troop balloon = new Troop();

    monkey.name = "Monkey " + i;
    flyingMonkey.name = "Flying Monkey " + i;
    wizard.name = "Wizard " + i;
    balloon.name = "Balloon " + i;

    monkey.health = 50;
    flyingMonkey.health = 50;
    wizard.health = 60;
    balloon.health = 55;

    army1.Add(monkey);
    army1.Add(flyingMonkey);
    army1.Add(wizard);
    army1.Add(balloon);
}

EDIT: By the way, at the current stage of your code and your classes, you could easily write this without any variables for creating individual troops at all: 编辑:顺便说一句,在您的代码和类的当前阶段,您可以轻松编写此代码,而无需任何变量来创建单个部队:

for (int i=0; i<5; i++){
    army1.Add(new Troop
    {
        name = "Monkey " + i,
        health = 50
    });
    army1.Add(new Troop
    {
        name = "Flying Monkey " + i,
        health = 50
    });
    army1.Add(new Troop
    {
        name = "Wizard " + i,
        health = 60
    });
    army1.Add(new Troop
    {
        name = "Balloon " + i,
        health = 55
    });
}

You have 4 set of wizards,monkey,flyingmonkey and baloon (20 Troop items). 您有4组向导,猴子,飞行猴子和气球(20个部队物品)。

 for(int i=0;i<5;i++)
 {
             army1.Add(monkey);
             army1.Add(flyingMonkey);
             army1.Add(wizard);
             army1.Add(balloon);
 }

This would mean, each of them has to die 4 times, before not appearing again. 这意味着他们每个人必须死4次,然后才能再次露面。 But in your non-Class code, you just have 4 of them. 但是在您的非Class代码中,您只有4个。

var health = new List<int>() {50,50,60,55};

This explains why the Wizard dies earlier in the non-class code, while it keeps coming back(well, in reality it doesn't it is another instance of wizard) 这就解释了为什么向导会在非类代码中早死,而又不断返回(嗯,实际上,它不是向导的另一个实例)

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

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