Java 使用变量类名而不是大量的if子句?
我现在被困住了,不知道更简单的解决办法,也许你能帮我 我有一个叫做Animal的接口和很多实现它的Animal类。 编辑:界面必须是错误的,然后:Java 使用变量类名而不是大量的if子句?,java,android,class,interface,Java,Android,Class,Interface,我现在被困住了,不知道更简单的解决办法,也许你能帮我 我有一个叫做Animal的接口和很多实现它的Animal类。 编辑:界面必须是错误的,然后: public interface Animals { Integer lifespan = 0; public Integer getLifespan(); } 在一个函数中,我得到一些随机的动物对象,我想得到它的变量 if (animal instanceof GuineaPig) { lifespan
public interface Animals {
Integer lifespan = 0;
public Integer getLifespan();
}
在一个函数中,我得到一些随机的动物对象,我想得到它的变量
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();
}
现在我需要对每种动物都有if条款,一定有更简单的方法,对吗?我做错了什么
编辑2:
完整接口和类:
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_w", "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_w", "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;
}
}
在您的小代码示例中,您可以简单地让
animates
接口具有getLifespan()
、getAge()
和getValue()
方法,并避免强制转换和if语句:
lifespan = animal.getLifespan();
age = animal.getAge();
value = animal.getValue();
您没有显示界面的定义,但根据您的问题,动物界面可能已经有了所有这些方法
编辑:
您的
Animals
接口(顺便说一句Animal
是一个更好的名称)只定义getLifespan()
。如果向其中添加其他方法(假设实现此接口的所有类都有这些方法),则可以在不强制转换的情况下调用它们。您不会陷入困境。你忽视了你的问题
因为Animal是一个接口,所以不需要将它们转换为受尊重的实例类型。只是删除所有的条件和文字
lifespan = animal.getLifespan();
age = animal.getAge();
value = animal.getValue();
这应该和使用接口编程一样有效。根本不需要添加条件。只需使用抽象:
abstract class Animal {
abstract int getAge();
abstract int getValue();
...
}
并在Rabbit(etc)类中实现这些方法,或者您可以在Animal类中实现它们并对其进行扩展。您应该在Animal接口中声明getLifespan()、getAge()和getValue()方法。然后可以使用这个:
Animal animal = new Rabbit(); //for example. Can change to new GuineaPig()
lifespan = animal.getLifespan();
age = animal.getAge();
value = animal.getValue();
我发现了问题。不是接口出了问题,也不是类出了问题
显然我用的是原始的ArrayAdapter而不是普通的。通过一个通用适配器,我可以使用以下方法。。。我甚至不明白为什么,但不管怎样……所有的if子句都有相同的函数,比如年龄、值。。不需要你想要的if语句。。我想我也是这么想的,但它不可能再喜欢这个功能了。你可以对此发表评论。。没有答案@Eran@DanielScholz
animal
变量是否为animal
类型?如果是的话,它不需要浇铸就可以工作。另一方面,如果animal
的类型为Object
(可能是从某个原始集合类型获得的),则必须首先将其强制转换为animal
。是的,它的类型为Animals。我把它放在一个列表视图中:动物=(动物)getItem(位置);是的,那正是我所拥有的。接口定义了方法,类具有getter和setter。但是它不会让我不使用铸造就完成它。我也是这么想的,但它不能重新热爱这个功能。@DanielScholz你能用动物界面更新你的帖子吗?我更新了它,并向界面展示了它的寿命part@DanielScholz好啊为了消除这些条件,请将方法getAge和getValue方法移动到Interface,并让其实现所有其他具体类。当我这样做时,我得到并出错,Android Studio告诉我删除方法体。:/为什么会这样?