Java 如何传递需要Food类型参数的参数?
我正在编写一个基于继承演示的程序。我正在尝试编写一个异常,以便唯一可以传递到链接到Wolf类的Meat类的参数。本质上,我试图允许唯一可以传递到eating方法中的参数是一个名为Meat的食物变量。代码如下: 动物Java 如何传递需要Food类型参数的参数?,java,inheritance,Java,Inheritance,我正在编写一个基于继承演示的程序。我正在尝试编写一个异常,以便唯一可以传递到链接到Wolf类的Meat类的参数。本质上,我试图允许唯一可以传递到eating方法中的参数是一个名为Meat的食物变量。代码如下: 动物 abstract public class Animal { String name; int age; String noise; abstract public void makeNoise(); public String getName() { r
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!");
}
}
}
Main
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(“Wolfs eat”+wolfExample.eat(meatExample.getName()))时,我会得到一个不受支持的异常错误我想这可能是因为没有传入食物变量。期望的结果是传入一个食物变量(如Plants),抛出一条异常消息。非常感谢您对如何解决此问题的任何帮助。首先,您的食肉类和狼类是相同的
您尚未传递“meatExample”的名称
并尝试实例化肉类对象并在Food类中分配它
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
}
public static void main(String[] args) {
//Create the Meat Object by sending the name in constructor
Meat meatExample = new Meat("Chicken");
//other code
}
肉类类别:
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
}
public static void main(String[] args) {
//Create the Meat Object by sending the name in constructor
Meat meatExample = new Meat("Chicken");
//other code
}
main()方法:
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
}
public static void main(String[] args) {
//Create the Meat Object by sending the name in constructor
Meat meatExample = new Meat("Chicken");
//other code
}
你必须先修改你的动物和食物类,然后在你的主类中做一些其他的改变,你也许能够达到你想要达到的目标。以下是一些建议的修改:
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而不是meatExample.getName()。Meat-meatExample=new-Meat(//此处是Food参数?)
仍然需要传递一个我仍然不确定的Food参数。我认为应该与它正在查找Food变量和已传递的String变量这一事实分开,因此我收到错误消息String无法转换为Food
?