简体   繁体   English

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

[英]using variable class names instead of lots of if clauses?

I'm trapped right now and don't know an easier solution to this, maybe you can help me out. 我现在被困,不知道更容易解决这个问题,也许你可以帮助我。

I have an Interface called Animal and lots of Animal Classes implementing it. 我有一个名为Animal的接口和许多实现它的动物类。 EDIT: The interface must be wrong then: 编辑:接口必须是错误的:

public interface Animals {
    Integer lifespan = 0;

    public Integer getLifespan();
} 

In a function I get some random animal Object back and I want to get variables of it. 在一个函数中,我得到一些随机的动物对象,我想得到它的变量。

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 have if clauses for every animal, there must be an easier way, right? 现在我需要为每一种动物设置if子句,必须有一种更简单的方法,对吧? What am I doing wrong? 我究竟做错了什么?

EDIT2: Full Interface and Classes: 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_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;
}
}

In your small code sample, you can simply let the Animals interface have getLifespan() , getAge() and getValue() methods, and avoid the casting and the if statements : 在您的小代码示例中,您可以简单地让Animals接口具有getLifespan()getAge()getValue()方法,并避免使用转换和if语句:

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

You didn't show the definition of your interface, but based on your question, it's possible that the Animal interface already has all those methods. 您没有显示界面的定义,但根据您的问题,Animal界面可能已经拥有所有这些方法。

EDIT : 编辑:

Your Animals interface (BTW Animal would be a better name) only defines getLifespan() . 你的Animals界面(BTW Animal会是一个更好的名字)只定义了getLifespan() If you add the other methods to it (assuming all the classes that implement this interface have these methods), you'll be able to call them without casting. 如果你向它添加其他方法(假设所有实现此接口的类都有这些方法),你将能够在不进行强制转换的情况下调用它们。

You are not trapped. 你没有被困。 You overlooked at your problem. 你忽略了你的问题。

Since Animal is an Interface, you need not to cast them to respected Instance types. 由于Animal是一个接口,因此您无需将它们转换为受尊重的实例类型。 Just removing all the conditions and writing 只是删除所有条件和写作

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

That should work as you are doing programming with interfaces. 这应该适用于您使用接口进行编程。 No need to add conditions at all. 根本不需要添加条件。

Just use abstraction: 只需使用抽象:

abstract class Animal {
    abstract int getAge();
    abstract int getValue();
...
}

And implements this methods in Rabbit(etc) classes or you can implement them in Animal class and just extend it. 并在Rabbit(etc)类中实现此方法,或者您可以在Animal类中实现它们并只是扩展它。

you should declare getLifespan(), getAge() and getValue() methods in Animal interface. 你应该在Animal界面中声明getLifespan(),getAge()和getValue()方法。 And then can use this: 然后可以使用这个:

Animal animal = new Rabbit(); //for example. Can change to new GuineaPig()
lifespan = animal.getLifespan();
age = animal.getAge();
value = animal.getValue();

I found the problem. 我发现了这个问题。 It wasn't the Interface at fault or the classes... 这不是故障界面或类......

Apparently I used a raw ArrayAdapter instead of a generic one. 显然我使用了原始的ArrayAdapter而不是通用的ArrayAdapter。 With a generic adapter it let me use the methods... I don`t even understand why, but whatever... 使用通用适配器让我使用这些方法...我甚至不理解为什么,但无论如何......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM