简体   繁体   中英

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. I want all the monkeys to have different instances, but without making different variables like monkey1, monkey2, or without using an array. All the monkeys are taking damage in different amounts, but right now, the damage is stacking up.

Code: https://dotnetfiddle.net/YRukEz

The cause for the different output is the way you populate your list of Troop objects:

 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). Accordingly, each of your Troop elements has to be removed five times until they are not in the list anymore.

Simply remove the loop, and the resulting number of lines from Code 2 will match that generated by Code 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).

 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. But in your non-Class code, you just have 4 of them.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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