加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用变量类名而不是许多if子句?

发布时间:2020-12-15 04:33:01 所属栏目:Java 来源:网络整理
导读:我现在被困,不知道更容易解决这个问题,也许你可以帮助我. 我有一个名为Animal的接口和许多实现它的动物类. 编辑:接口必须是错误的: public interface Animals { Integer lifespan = 0; public Integer getLifespan();} 在一个函数中,我得到一些随机的动物
我现在被困,不知道更容易解决这个问题,也许你可以帮助我.

我有一个名为Animal的接口和许多实现它的动物类.
编辑:接口必须是错误的:

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子句,必须有一种更简单的方法,对吧?我究竟做错了什么?

EDIT2:
完整的接口和类:

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;
}
}

解决方法

在您的小代码示例中,您可以简单地让Animals接口具有getLifespan(),getAge()和getValue()方法,并避免使用转换和if语句:

lifespan = animal.getLifespan();
age = animal.getAge();
value = animal.getValue();

您没有显示界面的定义,但根据您的问题,Animal界面可能已经拥有所有这些方法.

编辑:

你的动物界面(BTW Animal会是一个更好的名字)只定义了getLifespan().如果你向它添加其他方法(假设所有实现此接口的类都有这些方法),你将能够在不进行强制转换的情况下调用它们.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读