如何传递需要参数类型为Food的参数?
我正在编写一个基于继承演示的程序。我正在尝试编写一个异常,以便唯一可以传递到链接到Wolf类的Meat类的参数。本质上,我试图让唯一可以传入进食方法的参数成为一个名为Meat的Food变量。下面是代码:如何传递需要参数类型为Food的参数?
动物
abstract public class Animal
{
String name;
int age;
String noise;
abstract public void makeNoise();
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
abstract public Food eat(Food x) throws Exception;
}
食品
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()));
时,我得到了一个不受支持的异常的错误,我认为这可能是因为Food变量没有被传入。期望的结果是传递了一个Food变量,例如Plants,它将引发异常消息。任何帮助如何解决这个表示赞赏,谢谢。
你将不得不到m首先让你的动物和食物课程变得有趣,然后在你的主要课程中进行一些其他的改变,你可能能够实现你想要的。这里有几个建议的变化:
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();
}
}
}
所以,如果你打电话给wolfExample.eat(vegFood);
你的代码会抛出异常。
首先,你的食肉类和狼类是一样的。
你还没有为通过名你的“meatExample”
并尝试实例肉对象,并在食品类分配给它
Food meatExample = new Meat("Beef");
这样你调用getName()食品类的方法,而不是来自肉类。
基本上,你的Food
和Meat
类设计不正确,需要被固定如下所示,即Meat
类应该采取食品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
}
我认为应该分开的事实,它正在寻找一个食物变量和一个字符串变量已通过,所以我得到错误消息'字符串不能转换为食物'? – James
你应该修复你搞砸的缩进。 – khelwood
只是传递meatExample而不是meatExample.getName()。 –
'肉类肉类例子=新肉类(//食物争论在这里?)'仍然需要食物论证来传递,我仍然不确定。 – James