あなたが以下を持っている場合
- インターフェイス: 食事
- ホットドッグは食事を実装します
- バーガーは食事を実装します
- サラダ 具材 食事
これらのオブジェクト タイプの 1 つを取り込んで適切なオブジェクトを返すメソッドをどのように作成しますか?
例:
Method makeFood(HotDog)
if(HotDog instanceof Meal)
return new HotdogObject();
これをどのように正しく行うのですか?
私は一緒に働いています
static public Food createMeal(Meal f)
throws Exception
{
if (f instanceof Hotdog)
{
return f = new HotDog();
}
if (f instanceof Burger)
{
return f = new Burger();
}
throw new Exception("NotAFood!");
}