Home > database >  how to store multiple different classes into one array
how to store multiple different classes into one array

Time:10-27

I am trying to make a pokemon text based clone for a school project and I need to store the 2 different pokemon that are fighting in a single array so I can easily compare and exchange stats instead of using thousands of if statements for each possible combination of pokemon classes.

nothing, i cannot figure out how to store different classes into one single array if the arrays can only be made from one class

CodePudding user response:

I recommend making each Pokemon extend an abstract class Pokemon

public abstract class Pokemon {
    // Variables all Pokemon share
    int level;
    int maxHP;
    int currentHP;
    String type;
    ArrayList<Attack> attacks;
    
    public boolean attack(int num){
        ...
    }
}

public class Charmander extends Pokemon {
    public Charmander(int level, int maxHP){
        this.level = level;
        this.maxHP = maxHP;
        this.currentHP = maxHP;
        this.type = "Fire";
    }
}

Then, you could make an Array such as Pokemon[] myPokemon that holds different Pokemon

  • Related