2016-11-23 105 views
-1

我正在編寫一個基於繼承演示的程序。我正在嘗試編寫一個異常,以便唯一可以傳遞到鏈接到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,它將引發異常消息。任何幫助如何解決這個表示讚賞,謝謝。

+0

你應該修復你搞砸的縮進。 – khelwood

+0

只是傳遞meatExample而不是meatExample.getName()。 –

+0

'肉類肉類例子=新肉類(//食物爭論在這裏?)'仍然需要食物論證來傳遞,我仍然不確定。 – James

回答

0

你將不得不到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);你的代碼會拋出異常。

0

首先,你的食肉類和狼類是一樣的。

你還沒有爲通過名你的「meatExample」

並嘗試實例肉對象,並在食品類分配給它

Food meatExample = new Meat("Beef"); 

這樣你調用getName()食品類的方法,而不是來自肉類。

0

基本上,你的FoodMeat類設計不正確,需要被固定如下所示,即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 
} 
+0

我認爲應該分開的事實,它正在尋找一個食物變量和一個字符串變量已通過,所以我得到錯誤消息'字符串不能轉換爲食物'? – James

相關問題