简体   繁体   English

有两个“ InstanceOf”的类?

[英]Class with Two “InstanceOf”?

I am running into trouble writing code that requires a Fruit, a Banana, a Orange, a Edible, a Snowberry, and IceCream to be classes. 我在编写要求将Fruit,Banana,Orange,Edible,Snowberry和IceCream用作类的代码时遇到了麻烦。 In the test cases provided to me, I have to write code that makes a Orange and Banana to be an instanceof Fruit and Edible. 在提供给我的测试用例中,我必须编写使Orange和Banana成为Fruit and Edible实例的代码。

At this point my Banana and Orange should extend to an abstract class of Fruit. 在这一点上,我的香蕉和橙子应该扩展到水果的抽象类。

public abstract class Fruit {

    protected String color;
    protected double weight;

    public Fruit (String color, double weight) {
        this.color = color;
        this.weight = weight;
    }

    public Fruit(double weight) {
        this.weight = weight;
    }

    public Fruit() {
    }

    public abstract double getWeight();

    public abstract String getColor();

    public abstract double getCalories();

    public boolean equals(Object fruit) {
        if ((this.getColor() == (((Fruit) fruit).getColor())&& (this.getWeight() == ((Fruit) fruit).getWeight()))) {
            return true;
        }
        else {
            return false;
        }
    }
}

public class Orange extends Fruit {

    private String color = "orange";

    public Orange (String color, double weight) {
        super("orange", weight);
    }

        public Orange (double weight) {
            super(weight);
        }

        public double getWeight() {
            return weight;
        }

        public String getColor() {
            return color;
        }

        public double getCalories() {
            return weight * 5.0;
        }
    }


public class Banana extends Fruit{

    private String color = "yellow";

    public Banana (String color, double weight) {
        super("yellow", weight);
    }

    public Banana (double weight) {
        super(weight);
    }

    public double getWeight() {
        return weight;
    }

    public String getColor() {
        return color;
    }   

    public double getCalories() {
        return weight * 10.0;
    }
}

Then, there is IceCream to be an "instanceof" my Edible. 然后,有IceCream成为“我的食用”的“实例”。 The IceCream extends to Edible, has no parameters in it's constructor, and has a getCalories() method to return 1000. From this information in the test case, I assume since it's an instance of the Edible class, the Edible class should also have no parameters in it's constructor as well? IceCream扩展到Edible,在其构造函数中没有参数,并且具有getCalories()方法以返回1000。根据测试案例中的此信息,我认为由于它是Edible类的实例,因此Edible类也应该没有它的构造函数中的参数也是如此?

After that, I have a Snowberry that extends to the Fruit class, has a super of "white" and .0117, a getCalories() that returns 1, and a getColor(), and getWeight() methods. 在那之后,我有一个扩展到Fruit类的Snowberry,其超级值为“ white”和.0117,一个getCalories()返回1,还有getColor()和getWeight()方法。 The problem here is that I need to have Snowberry to be an instance of Fruit, but not an instance of Edible. 这里的问题是我需要让Snowberry成为F​​ruit的实例,而不是Edible的实例。

The following test case is having me stumped to write code for: 下面的测试用例让我为下面的代码编写代码:

    public void test_IceCream() {
        IceCream ic = new IceCream();
        assertEquals(1000.0, ic.getCalories(), 0.0001);
        assertFalse(Fruit.class.isAssignableFrom(ic.getClass()));
        assertTrue(ic instanceof Edible);
    }

    public void test_Snowberry() {
        Snowberry sb = new Snowberry();
        assertEquals(0.117, sb.getWeight(), 0.0001);
        assertEquals("White", sb.getColor());
        assertFalse(sb instanceof Edible);
        assertTrue(sb instanceof Fruit);

From these test cases, I have got only IceCream to pass the JUnit3 test, but then run into trouble with the Banana and Orange to pass the test because it's not an instance of Edible: 从这些测试用例中,我只有IceCream通过JUnit3测试,但是由于Banana和Orange不是Edible实例,因此遇到了香蕉和Orange通过测试的麻烦:

 public void test_Orange() {
        Orange orange = new Orange(8);
        assertEquals(40.0, orange.getCalories(), 0.0001);
        assertTrue(orange instanceof Fruit);
        assertTrue(orange instanceof Edible);
    }

    public void test_Banana() {
        Banana banana = new Banana(5);
        Orange orange = new Orange(8);
        assertEquals(50.0, banana.getCalories(), 0.0001);
        assertTrue(banana instanceof Fruit);
        assertFalse(banana.getColor().equals(orange.getColor()));
        assertTrue(banana instanceof Edible);

As of now, my Edible class looks like this, because I am not sure how to approach this situation: 到目前为止,我的Edible类看起来像这样,因为我不确定如何处理这种情况:

public abstract class Edible {

    public Edible() {
    }
}

Last thing to mention, is that the Edible class is required to be a class (I think) because there is a testcase that checks if it's a class, and it requires Edible to have No constructors. 最后要提到的是,Edible类必须是一个类(我认为),因为有一个测试用例可以检查它是否是一个类,并且它要求Edible没有构造函数。

    public void test_Edible() {
        try {
            // Edible should not have any constructor
            Class<Edible> clazz = Edible.class;
            Constructor<?>[] ctors = clazz.getConstructors();
            assertEquals(0, ctors.length);

            // but it should have a way to getCalories
            Method m = clazz.getMethod("getCalories", new Class[0]);
            assertNotNull(m);
            assertEquals(Double.TYPE, m.getReturnType());
        }
        catch (Exception e) {
            // not supposed to happen
            e.printStackTrace();
            fail(e.getMessage());
        }
    }

To conclude this, I would greatly appreciate any tips on what I should change in my Edible class to make sure that Snowberry is an instance of Fruit and not Edible, Banana and Orange to be an instance of Fruit and Edible, IceCream to be just an instance of Edible, and for Edible to have 0 constructors. 总结一下,对于在我的食用类中应该进行哪些更改,我将不胜感激,以确保Snowberry是Fruit而不是Edible的实例,Banana and Orange是Fruit and Edible的instance,IceCream只是一个Edible的实例,并且Edible具有0个构造函数。

NOTES: 笔记:

  • I have tried extending Fruit to Edible, and got the Orange and Banana to completely pass the JUnit3 test, but then got stuck at Snowberry failing the test since it's a Fruit but not Edible. 我曾尝试将Fruit扩展到Edible,并让Orange和Banana完全通过JUnit3测试,但是后来由于失败了,因为它是Fruit但不是Edible,被困在Snowberry失败了。

  • According to my professor, the Instructions never stated that Fruit has to be extended to Edible, but this has confused be because a class cannot extend to 2 other classes, so I'm not sure how to get Banana and Orange to be instance of Edible. 根据我的教授的说法,《指示》从未说过必须将Fruit扩展到Edible,但这很困惑,因为一个类不能扩展到另外2个类,所以我不确定如何将Banana和Orange用作Edible的实例。 。

  • A hint from the instruction (I'm not sure how this can help): "All Edible objects has a way of computing how many calories they contain". 指令中的提示(我不确定这有什么帮助):“所有可食用对象都有一种计算它们所含卡路里的方式”。

  • The topic covers: A Class Hierarchy with Abstraction and Interfaces. 主题涵盖:具有抽象和接口的类层次结构。 (There's more to this problem that isn't mentioned, like a way to compare different Calories among other items, and that would be the interface for all the classes mentioned here. (这个问题还有很多未提及的问题,例如一种比较其他项目之间不同卡路里的方法,这将是此处提到的所有类的接口。

Sorry that this is a bit long, but I appreciate all the help I can get, and thank you taking the time to read all of this. 抱歉,这有点长,但是我很感谢我能获得的所有帮助,并感谢您抽出宝贵的时间阅读所有这些内容。 I figured if I provide as much detail, that you all may understand what my problem is. 我认为,如果我提供尽可能多的详细信息,那么大家可能都会明白我的问题所在。

You have several types of Fruit , but those aren't all Edible . 您有几种类型的Fruit ,但并非全部Edible You also have things that aren't Fruit , but are still Edible . 您还拥有并非Fruit东西,但仍可Edible That means those types aren't related. 这意味着这些类型无关。

You are right that a class can only extend one other class, but there is another way. 没错,一个类只能扩展另一个类,但是还有另一种方法。 You already mentioned the Edible class as it currently is doesn't add any concrete functionality. 您已经提到了Edible类,因为当前它没有添加任何具体功能。 That's a perfect use case for an interface instead instead of an abstract class. 这是接口而不是抽象类的完美用例。

The test case you mentioned allows this, because you can still refer to interface types as Class objects. 您提到的测试用例允许这样做,因为您仍然可以将接口类型称为Class对象。 This is also the only way to satisfy the "no constructors" requirement, as interfaces can't have constructors, but classes always have at least one. 这也是满足“无构造函数”要求的唯一方法,因为接口不能具有构造函数,但是类始终至少具有一个构造函数。 The test case also says your interface must define the getCalories method. 测试用例还说您的接口必须定义getCalories方法。

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

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