Question

Let's say I've got something like this :

public class Projectile
{
     public Projectile(){
     }

     public A(int i, int i2){
     //do stuff
     }
}

public class Bullet extends Projectile
{
     public Bullet(){
     }

     public Bullet(int i, int i2){
     }
}

public class Rocket extends Projectile
{
     public Rocket(){
     }

     public Rocket(int i, int i2){
     }
}

public class Weapon
{
     public Weapon(){
     }

     //This method is wrong and is where i need help
     public void fire(EntityProjectile projectile){
          projectile = new EntityProjectile(1,2);
     }
}

So I have a weapon and I want to put any projectile in the "fire" method. I'd like to be able to call the method like this way

fire(Bullet);
fire(Rocket);

or

fire(Bullet.class);
fire(Rocket.class);

So the code inside this method don't create the projectile class but the desired subclass. I know that I can overload the method by putting several "fire" method with different parameters but for example if I have 50 different projectiles subclasses, will I have to make 50 "fire" methods? or is there a way to have just one method?

EDIT: Okay I've just found how to do it !

public <T extends Projectile> void fire(Class<T> projectileClass)
{
    try 
    {
        T projectile = projectileClass.getConstructor(int.class, int.class).newInstance(1,2));              
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
}
Was it helpful?

Solution

You can use reflection and generics.

If you do it this way you must make sure that every class has an (int, int) constructor. Or you can alter the code to use interfaces / abstract methods.

Or have them take a String as an argument and parse the string.

public class Main {

    public static class Projectile
    {
         public Projectile(){
         }

         public Projectile(int i, int i2){
         }
    }

    public static class Bullet extends Projectile
    {
         public Bullet(){
         }

         public Bullet(int i, int i2){
         }
    }

    public static class Rocket extends Projectile
    {
         public Rocket(){
         }

         public Rocket(int i, int i2){
         }
    }

    public static class Weapon
    {
         public Weapon(){
         }

         //This method is wrong and is where i need help
         public <E extends Projectile> E fire(Class<E> projectile) {
            try {
                return projectile.getConstructor(int.class, int.class).newInstance(1, 2);
            } catch(Exception e) {
                System.out.println("The class " + projectile.getSimpleName() + " does not have a valid constructor");
                return null;
            }
         }
    }

    public static void main(String[] args) {
        Weapon weapon = new Weapon();
        Projectile p = weapon.fire(Projectile.class);
        Bullet b = weapon.fire(Bullet.class);
        Rocket r = weapon.fire(Rocket.class);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top