Java – use variable class names instead of many if clauses?
I'm trapped now. I don't know it's easier to solve this problem. Maybe you can help me
I have an interface called animal and many animal classes that implement it Edit interface must be wrong:
public interface Animals { Integer lifespan = 0; public Integer getLifespan(); }
In a function, I get some random animal objects, and I want to get its variables
if (animal instanceof GuineaPig) { lifespan = ((GuineaPig) animal).getLifespan(); age = ((GuineaPig) animal).getAge(); value = ((GuineaPig) animal).getValue(); } if (animal instanceof Rabbit) { lifespan = ((Rabbit) animal).getLifespan(); age = ((Rabbit) animal).getAge(); value = ((Rabbit) animal).getValue(); }
Now I need to set the if clause for each animal. There must be a simpler way, right? What on earth did I do wrong?
Edit2: complete interfaces and classes:
public interface Animals { final Integer id = 0; Integer prize = 999999; Integer value = 0; Integer age = 0; Integer lifespan = 0; String[] colors = { "c_bw","c_w","c_brw" }; String name = null; String finalColor = null; public String[] getColors(); public Integer getPrize(); public Integer getId(); public Integer getLifespan(); public Integer getAge(); public Integer getValue(); public String getName(); public String setName(String animalName); public String setFinalColor(String finalColor); } class GuineaPig implements Animals { private final Integer id = 0; private Integer prize = 10; private final Integer difficulty = 0; // easy private final Integer licenceNeeded = 0; private Integer value = 5; private Integer age = 0; private String[] colors = { "c_bw","c_brw" }; private String name = null; private String finalColor = null; @Override public Integer getPrize() { return prize; } public void setPrize(Integer prize) { this.prize = prize; } public Integer getDifficulty() { return difficulty; } public Integer getLicenceNeeded() { return licenceNeeded; } @Override public String[] getColors() { return colors; } public Integer getId() { return id; } @Override public Integer getLifespan() { return null; } public String getName() { return name; } public String setName(String name) { this.name = name; return name; } public String getFinalColor() { return finalColor; } public String setFinalColor(String finalColor) { this.finalColor = finalColor; return finalColor; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } class Rabbit implements Animals { private final Integer id = 1; private Integer prize = 15; private Integer lifespan = 30; private Integer difficulty = 0; // easy private final Integer licenceNeeded = 1; private Integer value = 7; private Integer age = 0; private String[] colors = { "c_b","c_br" }; private String name = null; private String finalColor = null; @Override public Integer getPrize() { return prize; } public void setPrize(Integer prize) { this.prize = prize; } public Integer getDifficulty() { return difficulty; } public Integer getLicenceNeeded() { return licenceNeeded; } @Override public String[] getColors() { return colors; } public Integer getId() { return id; } @Override public Integer getLifespan() { return null; } public String getName() { return name; } public String setName(String name) { this.name = name; return name; } public String getFinalColor() { return finalColor; } public String setFinalColor(String finalColor) { this.finalColor = finalColor; return finalColor; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
Solution
In your small code example, you can simply let the animals interface have getlifespan(), getage() and getvalue() methods, and avoid using transformation and if statements:
lifespan = animal.getLifespan(); age = animal.getAge(); value = animal.getValue();
You do not have the definition of the display interface, but according to your problem, the animal interface may already have all these methods
Edit:
Your animal interface (btw animal would be a better name) only defines getlifespan() If you add other methods to it (assuming that all classes implementing this interface have these methods), you will be able to call them without casting