samedi 30 avril 2022

Best way to retrieve the objects from variable arguments

I have an interface:

public interface Animal
{
   public Object info(Object ... o);
}

One of it's implementation :

public class Cat implements Animal
{
   public Object info(Object ... o)
   {
     //Extracting the information passed
     String a1 = (String)o[0];
     int b1 = (int)o[1];
     boolean c1 = (boolean)o[2];
   }
}

The datatype of a, b and c is always String, int and boolean respectively from place where the info method of Cat is called.

My question was that is their some neater way of extracting these objects and typecasting into the original form ?

Following solution would have been best but it cannot be implemented in java :

public class Cat implements Animal
{
  public Object info(String a1, int b1, boolean c1)
  {
    
  }
}

Aucun commentaire:

Enregistrer un commentaire