简体   繁体   English

如何使用对象类型作为方法/构造函数的参数?

[英]How do you use an object type as a parameter for method/constructor?

I'm currently working on a Java TD game.我目前正在开发 Java TD 游戏。 I have a class called "Wave" that holds the enemy types to spawn (that all extend the class "Enemy") and the delay between the spawns.我有一个名为“Wave”的类,它保存要生成的敌人类型(它们都扩展了“敌人”类)以及生成之间的延迟。

public class Wave {

*I dunno what*[] enemyTypes;
int[] delays;
private Point spawnPoint;

Wave(*I dunno what*[] enemyTypes, int[] delays, Point spawnPoint) {

    this.enemyTypes = enemyTypes;
    this.delays = delays;
    this.spawnPoint = spawnPoint;

}

void spawnWave() {

    for (int i = 0; i < enemyTypes.length; i++) {

        try {

            Thread.sleep(delays[i]);

        } catch (InterruptedException e) {
          e.printStackTrace();
        }

        Point sP = new Point(spawnPoint);

        *The enemy type to spawn* enemy = new *The enemy type to spawn*.spawn(sP);

    }

}

} }

I don't know how to code the bits within the **我不知道如何对 ** 中的位进行编码

looks like a use case for factory pattern看起来像是工厂模式的用例
you can provide array of factories for your enemy classes你可以为你的敌人类提供一系列工厂

What about something like this?这样的事情怎么办?

You can get more information at Oracle Java Documentation您可以在Oracle Java 文档中获得更多信息

public class Wave<T extends Enemy> {

  List<T> enemyTypes;
  int[] delays;
  private Point spawnPoint;

  Wave(int[] delays, Point spawnPoint) {

    this.enemyTypes = new ArrayList<>();
    this.delays = delays;
    this.spawnPoint = spawnPoint;

  }

  boolean addEnemy(T enemy) {
    if(enemyTypes.contains(enemy)) {
      return false;
    }
    this.enemyTypes.add(enemy);
    return true;
  }

  void spawnWave() {

    for (int i = 0; i < enemyTypes.size(); i++) {

        try {

            Thread.sleep(delays[i]);

        } catch (Exception e) {}

        Point sP = new Point(spawnPoint);

        Enemy enemy = enemyTypes.get(i);
        enemy.spawn(sP);

    }
  }
}

暂无
暂无

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

相关问题 您如何知道要使用哪种类型的构造函数? - How do you know what type of constructor to use? 您如何在Java方法中使用构造函数参数? - How do you use a constructors parameter in a method in Java? 使用构造函数作为方法参数 - use a constructor as a method parameter 在spring中注入构造函数时,如何知道并注入参数是否为列表object? - When injecting a constructor in spring, how do you know and inject if the parameter is a list object? 在默认接口方法中使用对象的类型参数 - Use the type parameter of the object in a default interface method Java:如何强制 class 类型参数与泛型方法中指定的泛型类型相同? - Java: How do you enforce a class type parameter to be the same as generic type specified in a generic method? 为什么不能将GregorianCalendar用作对象类型作为构造函数参数? - Why can't I use GregorianCalendar as an object type as a constructor parameter? 如何对在具有未知 object 参数的构造函数的 Class 中工作的方法进行单元测试(Junit 4) - How to do a Unit test (Junit 4) to a method that works in a Class that has a Constructor with an unknown object parameter 您是否应该使用 object 作为该对象 class 中的方法的方法参数? - Should you use an object as a method parameter for a method within that objects class? 如何将泛型类型('Object myVar')作为参数传递来替换 class 类型('SomeClass.class')? - How do you pass a generic type ('Object myVar') as a parameter to replace a class type ('SomeClass.class')?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM