java - 将 Java 类构造函数添加到 ArrayList(扩展)

标签 java arraylist constructor

我的英语不是很好,但我会尽力解释我正在尝试做的事情。
所以我在这里有这个类构造函数:

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);
}

暂时还可以吧? 如果不是,请纠正我...
所以让我们假设这没问题,我创建了一些扩展构造函数 God 的类,它们是 Vulcan、Loki 和 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);
}

所以现在 Vulcan 类将被添加到 gods ArrayList 中,对吗?如果我错了请纠正我。
好的,当我打印列表 gods 时,我得到一个空的 ArrayList。
我做错什么了吗?还是我必须定义类 Vulcan、Loki 和 Athena?我真的很困惑,请帮忙。

最佳答案

当你说 John、Michael 和 Levi 时,在我看来他们应该是 Human 类的一个实例,而不是完全不同的类。

可能你需要这样的东西,但不确定:

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());
    }
}

关于java - 将 Java 类构造函数添加到 ArrayList(扩展),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33504449/

相关文章:

java - 重复数字数组列表

java - 修改和访问线程类之外的值

Javascript function() 文字重载

java - 如何用java删除图像中的白色像素

java - Logstash-Logback编码器: “IllegalAccessError: failed to access class […]”

java - 如何重写 Java 中 ArrayList 的 toString 方法?

java - 以不同方式从另一个对象创建一个对象的设计模式

java - do-while 循环不是一直在工作

java - 如何在 SWT 中将文本设置为只读组合

java类调用/调用jsp文件