我正在尝试捕获应该显示错误消息的异常,但似乎没有这样做。我已经创建了另一个代码块,除了一些变量名称的更改之外完全相同,并且捕获异常但是这个代码似乎没有这样做。该例外的目的是该程序应该寻找一个Plant对象,如果没有找到它应该抛出一个例外来表明食草动物只吃植物。以下是相关的代码:
主要方法
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
public Main() {
super();
}
Rabbit rabbitExample = new Rabbit();
public static void main(String[] args) {
Rabbit rabbitExample = new Rabbit();
System.out.println("************EXCEPTION 2************");
try {
Food pork = new Food("Prok");
System.out.println("************Herbivore caught Exception example************");
System.out.println("Exception caught");
rabbitExample.eat(pork);
//wolfExample.eat(vegFood);
} catch (Exception e) {
// TODO: Add catch code
e.printStackTrace();
}
try {
System.out.println("************Herbivore non-caught Exception example************");
Food vegiFood = new Plant("Vegetables"); // you create a Meat object and store it in a Food variable (so to speak)
System.out.println("Herbivores eat " + rabbitExample.eat(vegiFood)); // must be surrounded by a try-catch block
}
catch (Exception ex) {
// TODO: Add catch code
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);;
}
}
}
动物类
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 Rabbit extends Herbivore
{
Rabbit()
{
name = "Haryy";
age = 2;
}
public void makeNoise()
{
noise = "Squeek!";
}
public String getNoise()
{
return noise;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String eat(String Food)
{
return Food;
}
public Food eat(Food x) throws Exception
{
if (x.equals(new Plant("Meat"))) {
throw new Exception("Herbivores only eat plants!");
} else {
return x;
}
}
}
草食动物课程
public class Herbivore extends Animal
{
public Food eat(Food x) throws Exception
{
if (x.equals(new Plant("Meat"))) {
throw new Exception("Herbivores only eat plants!");
} else {
return x;
}
}
public void makeNoise()
{
noise = "Woof!";
}
public String getNoise()
{
return noise;
}
}
食品类
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;
}
@Override
public String toString() {
return name;
}
}
我是关于显示的代码量的辩护人,但我想显示运行程序所需的所有相关代码。输出如下所示:
************EXCEPTION 2************
************Herbivore caught Exception example************
Exception caught
************Herbivore non-caught Exception example************
Herbivores eat Vegetables
而输出应为:
************EXCEPTION 2************
************Herbivore caught Exception example************
Exception caught
java.lang.Exception: Herbivores only eat plants!
************Herbivore non-caught Exception example************
Herbivores eat Vegetables
感谢任何帮助,谢谢。
答案 0 :(得分:2)
Hetre是你的问题:
public Food eat(Food x) throws Exception { if (x.equals(new Plant("Meat"))) { throw new Exception("Herbivores only eat plants!"); } else { return x; } }
您的食物子类不会覆盖equals()
(和hashcode()
)。类Object
中的默认实现快速执行==
比较,即使它们在逻辑上相同,对于不同的对象也始终为false。
但是要抵制在课堂equals()
中实施hashcode()
(和Food
)的诱惑。只有具体的类(你创建对象的类)才能真正判断某个其他对象是否相等。
答案 1 :(得分:0)
此if (x.equals(new Plant("Meat")))
将始终产生false(除非x参数作为null传递),因此永远不会触发您的异常,因为新Plant在内存中创建了一个新引用,并且它不能等于已经存在的参考文献