如何传递一个需要Food类型参数的参数?

时间:2016-11-23 21:02:55

标签: java inheritance

我正在编写一个基于继承演示的程序。我正在尝试编写一个异常,以便唯一可以传递给Meat类的参数,该类链接到类Wolf。本质上,我试图允许唯一可以传递给eating方法的参数是一个名为Meat的Food变量。这是代码:

动物

public class Food {

    //field that stores the name of the food
    public Food name; 

    //constructor that takes the name of the food as an argument
    public Food(Food name){
        this.name = name;
    }

    public Food getName() {
        return name;
    }
}

食品

public class Meat extends Food 
{

    public Meat(Food name) 
    {
        super(name);
    }

    public Food getName() 
{
    return super.getName();
}
}

public class Wolf extends Carnivore
{

Wolf()   
{
    name = "Alex";
    age = 4;

}
    public void makeNoise()  
    {
        noise = "Woof!";
    }
    public String getNoise()  
    {
        return noise;
    }
    public String getName() 
    {
        return name;
    }  
    public int getAge()
    {
        return age;
    }

    public Food eat(Food x) throws Exception
    { 
        if (x instanceof Meat) {
                return x;
            } else {
               throw new Exception("Carnivores only eat meat!");
            }
    }    
}

食肉动物

public class Wolf extends Carnivore
{

Wolf()   
{
    name = "Alex";
    age = 4;

}
    public void makeNoise()  
    {
        noise = "Woof!";
    }
    public String getNoise()  
    {
        return noise;
    }
    public String getName() 
    {
        return name;
    }  
    public int getAge()
    {
        return age;
    }

    public Food eat(Food x) throws Exception
    { 
        if (x instanceof Meat) {
                return x;
            } else {
               throw new Exception("Carnivores only eat meat!");
            }
    }
}

public class Main {

    public static void main(String[] args) 
    {

        Wolf wolfExample = new Wolf();        
        System.out.println("************Wolf\"************");
        System.out.println("Name = " + wolfExample.getName());
        System.out.println("Age = " + wolfExample.getAge());
        wolfExample.makeNoise();
        System.out.println("Noise = " + wolfExample.getNoise());

        Meat meatExample = new Meat(//Food argument goes here?);
        System.out.println("************Wolf eating habits************");
        System.out.println("Wolves eat " + wolfExample.eat(meatExample.getName()));
    }
}

主要

System.out.println("Wolves eat " + wolfExample.eat(meatExample.getName()));

我遇到的问题是我无法在我的main方法中创建的新Meat对象中传递任何食物参数。当我尝试调用.file时我得到了一个不受支持的异常的错误,我认为这可能是因为一个Food变量尚未被传入。期望的结果是传递了诸如植物之类的Food变量抛出异常消息。任何有关如何解决此问题的帮助表示赞赏,谢谢。

3 个答案:

答案 0 :(得分:0)

首先,你的Carnivore类和Wolf类是一样的。

您尚未传递'meatExample'的名称

尝试实例化Meat对象并在Food类中分配

Food meatExample = new Meat("Beef");

这样你就是调用Food类的getName()方法而不是Meat类。

答案 1 :(得分:0)

基本上,您的FoodMeat类设计不正确,需要修复如下所示,即Meat类应该采用food name属性的参数

食品类:

public abstract class Food {

    //field that stores the name of the food
    protected String name; 

    public String getName() {
        return this.name;
    }
}

肉类:

public class Meat extends Food {

    public Meat(String name) {
        super.name = name;
    }

    //other methods or add other specific meat fields
}

main()方法:

public static void main(String[] args) {

        //Create the Meat Object by sending the name in constructor
        Meat meatExample = new Meat("Chicken");
        //other code
}

答案 2 :(得分:0)

您必须首先修改您的动物和食物课程,然后在您的主课程中进行少量其他更改,您可能能够实现您的目标。以下是一些建议的更改:

data: ["key": key, "vrn": vpn, "password": password, "taxDue": taxDueString, "motDue": motDueString, "make": makeString, "date": dateString, "manufacture": manufactureString, "cc": ccString, "co2": co2String, "fuel": fuelString, "export": exportString, "status": statusString, "colour": colourString, "approval": approvalString, "wheelplan": wheelplanString, "revenue": revenueString, "featureParking": parkingIsOn, "featureMot": motIsOn, "featureTolls": tollsIsOn, "featureInsurance": insuranceIsOn, "featureFuel": fuelIsOn, "featureParts": partsIsOn, "featureTraffic": trafficIsOn, "firstname": firstNameText.text, "lastname": lastNameText.text, "mobile": mobileText.text, "email": emailText.text ]

因此,如果您致电public class Food { //field that stores the name of the food public String name; //constructor that takes the name of the food as an argument public Food(String name){ this.name = name; } public String getName() { return name; } } public class Meat extends Food { public Meat(String name) { super(name); } public String getName() { return super.getName(); } } public class Main { public Main() { super(); } public static void main(String[] args) { Wolf wolfExample = new Wolf(); System.out.println("************Wolf\"************"); System.out.println("Name = " + wolfExample.getName()); System.out.println("Age = " + wolfExample.getAge()); wolfExample.makeNoise(); System.out.println("Noise = " + wolfExample.getNoise()); try { Meat meatExample = new Meat("Steak"); //Food vegFood = new Food("Spinach"); System.out.println("************Wolf eating habits************"); wolfExample.eat(meatExample); //wolfExample.eat(vegFood); } catch (Exception e) { // TODO: Add catch code e.printStackTrace(); } } } ,您的代码将抛出异常。