PepsiGam3r
PepsiGam3r

Reputation: 302

Adding Java Class Constructor To An ArrayList (extends)

I'm not very good at English, but I'll try to explain what I'm trying to do.
So I have this class constructor here:

public class God {

public static ArrayList<God> gods = new ArrayList<God>();

private String name;
private PowerType powerType;
private AttackType attackType;
private ArrayList<ItemStack> abilities;
private Pantheon pantheon;
private GodClass godClass;
private List<Pro> pros;
private int favorCost;
private int gemCost;

public enum Pro {
    HIGH_SINGLE_TARGET_DAMAGE, HIGH_MOBILITY, HIGH_AREA_DAMAGE, HIGH_CROWD_CONTROL, HIGH_DEFENSE, HIGH_SUSTAIN, PUSHER, HIGH_ATTACK_SPEED, HIGH_MOVEMENT_SPEED, GREAT_JUNGLER, MEDIUM_CROWD_CONTROL,
}

public enum GodClass {
    ASSASSIN, GUARDIAN, HUNTER, MAGE, WARRIOR
}

public enum Pantheon {
    CHINESE, EGYPTIAN, GREEK, HINDU, MAYAN, NORSE, ROMAN
}

public enum PowerType {
    PHYSICAL, MAGICAL
}

public enum AttackType {
    MELEE, RANGED
}

private int health;
private int mana;
private int speed;
private int range;
private double attackSpeed;
private int damage;
private int physicalProtection;
private int magicalProtection;
private int hp5;
private int mp5;

public God(String name, PowerType powerType, AttackType attackType,
        ArrayList<ItemStack> abilities, Pantheon pantheon,
        GodClass godClass, List<Pro> pros, int favorCost, int gemCost,
        int health, int mana, int speed, int range, double attackSpeed,
        int damage, int physicalProtection, int magicalProtection, int hp5,
        int mp5) {
    this.name = name;
    this.powerType = powerType;
    this.attackType = attackType;
    this.abilities = abilities;
    this.pantheon = pantheon;
    this.godClass = godClass;
    this.pros = pros;
    this.favorCost = favorCost;
    this.gemCost = gemCost;
    this.health = health;
    this.mana = mana;
    this.speed = speed;
    this.range = range;
    this.attackSpeed = attackSpeed;
    this.damage = damage;
    this.physicalProtection = physicalProtection;
    this.magicalProtection = magicalProtection;
    this.hp5 = hp5;
    this.mp5 = mp5;
    gods.add(this);
}

This is okay for now, right? If not, correct me please...
So let's suppose this is okay, I created some classes that extends the constructor God which are Vulcan, Loki and Athena

public class Vulcan extends God {

private Vulcan(String name, PowerType powerType, AttackType attackType,
        ArrayList<ItemStack> abilities, Pantheon pantheon,
        GodClass godClass, ArrayList<Pro> pros, int favorCost, int gemCost,
        int health, int mana, int speed, int range, double attackSpeed,
        int damage, int physicalProtection, int magicalProtection, int hp5,
        int mp5) {
    super("VULCAN", PowerType.MAGICAL, AttackType.RANGED, abilities,
            Pantheon.ROMAN, GodClass.MAGE, Arrays.asList(
                    Pro.HIGH_AREA_DAMAGE, Pro.PUSHER), 5500, 200, 380, 245,
            360, 55, 0.9, 34, 13, 30, 7, 5);
}

So now Vulcan class is going to be added to the gods ArrayList, right? correct me if I'm wrong.
Okay so when I print the list gods, I get an empty ArrayList.
Did I do something wrong? Or do I have to define the classes Vulcan, Loki and Athena? I'm really confused, help please.

Upvotes: 1

Views: 254

Answers (3)

ccc
ccc

Reputation: 370

This code can use to pass values to super class constructor by using super() statement on class John.

class John extends Human {

public John(String name, int old) {
    super(name, old);
}

public static void main(String[] args) {
    new John("John", 23);
    new John("Michael", 31);
    new John("Levi", 41);

}
}

Upvotes: -1

Pavan Dittakavi
Pavan Dittakavi

Reputation: 3181

It would make much sense to have John, Levi and Michael as an instance of Human class.

Human john = new Human( "John", 23 );
Human levi = new Human( "Levi", 24 );
Human michael = new Human( "Michael", 25 );

Upvotes: 1

Abubakkar
Abubakkar

Reputation: 15664

When you say John, Michael and Levi, it seems to me that they should be an instance of the class Human rather than completely different classes.

Probably you need something like this, but not sure :

public Human {
    public static ArrayList<Human> humans = new ArrayList<Human>();

    private String name;
    private int old;

    public Human(String name, int old) {
        this.name = name;
        this.old = old;
        humans.add(this);
    }

    public static void main(String args[])
    {
        Human john = new Human("John", 21);
        Human michael = new Human("Michael", 31);
        Human levi = new Human("Levi", 41);

        System.out.println(Human.humans.size());
    }
}

Upvotes: 3

Related Questions