2016-11-23 5 views
-1

私は、継承のデモンストレーションに基づいたプログラムを作成しています。クラスWolfにリンクされているMeatクラスに渡すことができる唯一のパラメータであるように、例外を記述しようとしています。本質的には、私は食べる方法に渡すことができる唯一のパラメータが肉という食べ物の変数であることを許可しようとしています。ここでは、コードです: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変数が渡されなかったからです。PlantsなどのFood変数が渡され、例外メッセージがスローされます。これを解決する方法についての助けに感謝します、ありがとうございます。

+0

あなたの混乱したインデントを修正する必要があります。 – khelwood

+0

は、meatExample.getName()の代わりにmeatExampleを渡すだけです。 –

+0

'肉の肉例=新しい肉(//食べ物の引数はここに入りますか?)'私はまだわかりません。 – James

答えて

0

ています最初にAnimal and FoodクラスをodifyしてからMainクラスでいくつかの変更を加えれば、あなたがしようとしていることを達成することができます。推奨される変更点は次のとおりです。

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

まず、CarnivoreクラスとWolfクラスは同じです。

あなたはあなたの「meatExample」の

を名前に合格し、あなたがではなく、食品クラスののgetName()メソッドを呼び出している。この方法は肉のオブジェクトをインスタンス化しようとする食品クラスで

Food meatExample = new Meat("Beef"); 

それを割り当てていませんMeatクラスから。

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

私はFood変数を探していて、String変数が渡されているので、 'StringはFoodに変換できません。 ' – James

関連する問題