How can I use objects of different classes in one array? (Java)

a friend of mine came up with an idea for a racing game and I am trying to create it in java. now i have made 3 classes for cars, 1 for slot machines, 1 for computers (ai) and a main one that contains some variables like location (x, y on screen) and name, to name a few. the first 2 are inherited from the last. I was hoping this would allow me to create a single array with both players and computer players. this however does not work and now my question is:

is there any way for them to have an array with different types of objects, and if possible, how can I do that or are there any tutorials on it?

I've looked at the interfaces, but I don't think it would do the trick, but please correct me if I'm wrong.

this was the idea that I had:

MainCar[] carsArray = new MainCar[totalPlayers];
for(int i = 0; i < totalHumanPlayers; i++)
{
  carsArray[i] = new PlayerCar();
}
for(int i = 0; i < totalComputerPlayers; i++)
{
  carsArray[i] = new ComputerCar();
}

      

The idea is that I can go through all the players (human and computer) to draw them in their places and decide who will be next:

Thanks a lot, and please forgive my english, I don't know if it is correct or not, not my first language :)

+3


source to share


4 answers


Okay, with the second loop, you are overwriting the previously set values, because you start at the same offset i = 0.

I would suggest something like:

for(int i = totalHumanPlayers; i < (totalHumanPlayers + totalComputerPlayers); i++)

      

You can keep both types of cars in your array just like you do (by declaring the array to be of type MainCar), remember that MainCar can be ComputerCar or PlayerCar, you will need to use to use one of (if you need to access specific members PlayerCar or ComputerCar), for example:

PlayerCar player = (PlayerCar)carsArray[x];

But in your case, if you only want the tools in MainCar, you can simply access them directly (assuming they are members of MainCar of course), for example:

carsArray[i].location.x



or

carsArrays[i].x

Remember to initialize the array correctly. (Don't overlap your data)

EDIT:

And to determine if a car has a particular type of instanceof operator, here's an example:

if (carVar instanceof PlayerCar) { PlayerCar player = (PlayerCar)carVar; }

+2


source


public class PlayerCar : MainCar {}
public class ComputerCar : MainCar {}



MainCar[] carsArray = new MainCar[totalPlayers];
for (int i = 0; i < totalCarsCount; i++) {
   if (carsArray[i] instanceof PlayerCar) {
       System.out.println("Player car");
   } else {
       System.out.println("Computer Car");
   }
}

      



+2


source


It might be nice to have the same array for player car and computer car, as long as they don't both inherit the same class. For example, it's okay if:

interface MainCar{
    public void horn();
}

class PlayerCar implements Car{
    public void run(){ System.out.println("Broom Broom");}
}

class ComputerCar implements Car{
    public void run(){ System.out.println("tak tak");}
}

MainCar[] cars = new MainCar[totalPlayers];
cars[0] = new PlayerCar();
cars[1] = new ComputerCar();

      

This is due to the joint variance of arrays in Java.

If PlayerCar

and ComputerCar

are not inherited MainCar

, then the above would have been possible (definitely not recommended). But due to coincidence, you could:

Object[] cars = new Object[totalPlayers];    
cars[0] = new PlayerCar();
cars[1] = new ComputerCar();

      

The reason this is not preferred is to access the cars[0]

above. You don't know what type will look like this:

Object a = cars[0];

      

a

may be String

, Animal

, List

or something like that. And ig you have to use it as a car, you have to type a roll, which is very very dangerous:

if(a instanceof PlayerCar){
 //then do something
}else if(a instanceof ComputerCar){
 //then do something
}

      

PS: Change your username from javaNoobsForever

, you can't be that bad :). Well, we are all noobs :)

+1


source


Make a car interface that all cars implement and then create an array of the interface type.

Note. You will only be able to call methods from the interface unless you return them to the appropriate classes

-1


source







All Articles