vendredi 7 mai 2021

How can I create random objects from a bunch of classes (herited classes)

I'm sorry if there is a similar question, tried searching but didn't find an adequate response for my use.

I have 5 classes that herits from one class (spaceshipclass) and I need to create random objects from each class and store them in a binary tree. with each one of the objects, two random integers as shown in the code bellow.

public class spaceships
{
    int cost;
    int combatPower;
    private Random rand = new Random();

     public spaceships(int cost, int combatPower)
    {
        this.cost = cost;
        this.combatPower = combatPower;

        cost = rand.Next(10000, 1000000);
        combatPower = rand.Next(20, 100);

    }

} 

public class Patrol : spaceships
{
        public Patrol (int cost,int combatPower) : base(cost,combatPower)
        {
        }
}

the second class is one of the inherited classes.

I hope someone can help me.




Aucun commentaire:

Enregistrer un commentaire