Ruman Khan Pathan
Ruman Khan Pathan

Reputation: 137

How to return multiple types from single method

I have a class named as Boss which contains different inner classes for diffrent types of bosses for my game . I have a Main class which uses a method getBoss() of Boss class ,to retrive appropriate boss class object as per current running level.

i.e. if level = 1 then "level1 boss" will arrive in the game. Boss class uses level variable to choose inner boss Classes. this is what i m trying to do, but the problem is getBoss() is made to return objects of inner classes of Boss

Class Boss extends Sprite{
 public static Level3Boss l3;
 public static Level1Boss l1;
 public stati  Level2Boss l2;

 //Constructor
 public Boss(int level){
  if(level == 1){
    l1 = new Level1Boss();
     }
  else if(level = 2){
    l2 = new Level2Boss();
   }else{
    l3 = new Level3Boss();
   }
 }

 Class Level1Boss extends Sprite{
   //XYZ Propeties of this boss
  }

 Class Level2Boss extends Sprite{
   //XYZ Propeties of this boss
 }

 Class Level3Boss extends Sprite{
   //XYZ Propeties of this boss
   }

   public /*What to type here*/ getBoss(){
        if(level == 1){
         return l1;
       }
      else if(level = 2){
        return l2;
       }else{
        return l3;
      }
    }

}

So the confusion is what to write there at the place of return_type

  1.Level1Boss for ex: public Level1Boss getBoss(){}

   2.Level2Boss for ex: public Level2Boss getBoss(){}

   3.Level3Boss for ex: public Level2Boss getBoss(){}

  4.or something else ?

Is there any way by which i can return any of this 3 objects of different classes from getBoss() method

One more thing i tried return_type "Object" it just works but it cant be used in setting the images and drawing it on the full screen window.

So thanks in advance

Upvotes: 1

Views: 429

Answers (8)

Renaud Mathieu
Renaud Mathieu

Reputation: 376

There is a relation between a Boss and a Level. You have to think about the relation then make your architecture.

Anyway, I suggest you to summarize (abstract) as much as possible your code and your architecture. Polymorphism in Java will help you.

public abstract class Boss extends Sprite {
 public int strength;
 public int name;
 ...
}

public class Level {
 public int number;
 public Boss boss;
 ...
}

Define some Boss

public class Megatron extends Boss{

}

You don't need to abstract a Level. An instance of this class should be enough for each level.

Boss megatron = new Megatron(100, "Megatron");
Level level1 = new Level(1, megatron);

Upvotes: 0

Simon Curd
Simon Curd

Reputation: 850

You're currently using the Boss class like a factory, so start by making that more declarative:

public class BossFactory
{
   private Map<Integer,Boss> bosses = new HashMap<Integer,Boss>();

   public Boss getBoss(int level)
   {
      Boss resultBoss = bosses.get(level);
      if (resultBoss == null)
      {
         // create the boss
         // ideally this would be dynamically instantiated, but  fine for now
         if (level == 1)
         {
             resultBoss = new Level1Boss();
         }
         else if (level == 2)
         {
             resultBoss = new Level2Boss();
         }
         // store the boss
         bosses.put(level, resultBoss);
      }
      return resultBoss;
   }
}

Then turn your level bosses into a proper hierarchy of objects:

class Boss extends Sprite { }
class Level1Boss extends Boss {}
class Level2Boss extends Boss {}

Upvotes: 2

Christie Louis Jones
Christie Louis Jones

Reputation: 13

Declare it in your final page and call direct the function names like this:

public static Level3Boss l3;
public static Level1Boss l1;
public static  Level2Boss l2;


    Level3Boss= Boss.Getl3();
    Level1Boss= Boss.Getl1();
    Level2Boss= Boss.Getl2();

Upvotes: 0

Alya&#39;a Gamal
Alya&#39;a Gamal

Reputation: 5638

First : you will return Sprite . like this :

 public Sprite getBoss() {
        if (level == 1) {
            return l1;
        } else if (level = 2) {
            return l2;
        } else {
            return l3;
        }
    }

Second : class in java written with small character , not Class

Finally : use == in if statement not = , like that :

 else if (level == 2)

Upvotes: 0

ilomambo
ilomambo

Reputation: 8350

You need to return the common parent of the 3 classes, Sprite and then cast it back to the original class

public Sprite getBoss(){
    if(level == 1){
        return l1;
    }
    else if(level = 2){
        return l2;
    } else{
        return l3;
    }
}

...
Sprite s = getBoss();
if( s instanceof Level1Boss ) {
    ((Level1Boss)s).whatever1();
} else if( s instanceof Level2Boss ) {
    ((Level2Boss)s).whatever2();
} else if( s instanceof Level3Boss ) {
    ((Level3Boss)s).whatever3();
} else {
    // error
}

It is similar to the popular method findViewById(id). It returns a View, but you can cast it to whatever widget is really is.

Upvotes: 0

Perneel
Perneel

Reputation: 3367

Make a class Boss and let Level1-2-3Boss extend it.

Example:

public abstract class Boss{
    public abstract int getX();
    public abstract int getY();
    public abstract int getZ();
}

public class Level1Boss extends Boss{
    ... (implement the abstract methods)
}

public Boss getBoss(){
    ...
}

Upvotes: 4

anshulkatta
anshulkatta

Reputation: 2064

You can return a Superclass , let say Level be a Superclass , and all level class extends it , then you can place Level as return type , as you said u tried with Object , but Object will not have methods of these class , so You can give Level as a superclass and all three level class extends it , now when u return and call any method on it , it will call the overridden method of that class , Level 1,2,3

Class Level {}

Class Level1Boss extends Level
{
        //its methods
}

Class Level2Boss extends Level
{
        //its methods
}

Class Level3Boss extends Level
{
        //its methods
}

now when you do this

public Level getBoss(){
        if(level == 1){
         return l1;
       }
      else if(level = 2){
        return l2;
       }else{
        return l3;
      }
    }

Then at runtime , this Level object reference will have the reference of its subclasses

Upvotes: 0

Michal Borek
Michal Borek

Reputation: 4624

You need to return Sprite, since it is common to each class.

I would prefer to create new subtype, or type interface (e.g. IBoss), which would group all "bosses". That would be more explicit and easy to understand solution.

Upvotes: 1

Related Questions