简体   繁体   English

创建子类的新实例

[英]Create new Instance of Child Class

I have an array list of Enemies and each enemy kind extends enemy. 我有一个敌人列表,每种敌人种类都会扩展敌人。 Now I don't wan't every same enemy kind to share all their stats, but I base my enemy selection of another array list. 现在,我不想让每种敌人都共享所有统计信息,但是我将敌人的选择作为另一个数组列表的基础。 So I think the way to go would be to get the object of the array list containing all the options and then changing them to new Instances of the same class. 因此,我认为方法是获取包含所有选项的数组列表对象,然后将其更改为同一类的新实例。 My question is, how would I do that? 我的问题是,我该怎么做? Or do you guys have a better approach? 还是你们有更好的方法?

For easy of understanding here's what I mean abstracted 为了易于理解,这里是我的抽象意思

class shop{
   ArrayList<Enemy> allEnemies;
}

class generator{
  ArrayList<Enemies> selectedToGenerate = based on some of allEnemies

  for(Enemy x : selectedToGenerate){ // i know this wouldn't work
    x = newInstanceOf(x.getNonenemyThereforeChildclassClass());
  }
}

hope this explains what I mean. 希望这能解释我的意思。 Appreciate your time! 感谢您的时间!

You can add a Builder to your Enemy . 您可以将Builder添加到您的Enemy

abstract class Enemy {
   private int strength;
   public Builder<Enemy> getBuilder();
   public static class Builder<T extends Enemy> {
       int str;
       public Builder<T> copyValues(T enemy) {
           str = enemey.strength;
           return this;
       }
       public Builder<T> strength(int s) {
           str = s;
           return this;
       }
       protected void fillValues(T toFill) {
           toFill.strength = str;
       }
       protected abstract T createInstance();
       public T build() {
           T result = createInstance();
           fillValues(result);
           return result;
       }
   }
}

This Builder can create instances of your Enemy and fill it with values. Builder可以创建您的Enemy实例,并用值填充它。 For subclasses, you can extend the Builder by allowing it to fill more values. 对于子类,可以通过允许其填充更多值来扩展Builder

class EnemyA extends EnemyA {
    private int speed;
    public Builder<EnemyA> getBuilder() {
        return new Builder();
    }

    class EnemyABuilder extends Builder<EnemyA> {
        int speed;
        public EnemyABuilder copyValues(EnemyA enemy) {
            super.copyValues(enemy);
            speed = enemy.speed;
        }
        public EnemyABuilder speed(int s) {
            speed = s;
            return this;
        }
        protected void fillValues(EnemyA toFill) {
            super.fillValues(toFill);
            toFill.speed = speed;
        }
        protected EnemyA createInstance() {
            return new EnemyA();
        }
    }
}

Now, you can create copies of the enemies by using their builders: 现在,您可以使用敌人的构建器来创建敌人的副本:

for(Enemy x : selectedToGenerate){ // i know this wouldn't work
    Builder<? extends Enemy> builder = x.getBuilder();
    builer.copyValues(x);
    Enemy copy = builder.build();
}

As an additional bonus, you can use the builder to quickly create different versions of the same enemy. 另外,您可以使用构建器来快速创建同一敌人的不同版本。

EnemyA.Builder base = new Builder().strength(10);
EnemyA withSpeed1 = base.speed(1).build();
EnemyA withSpeed2 = base.speed(2).build();    
EnemyA withSpeed3 = base.speed(3).build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM