我想看看是否可以将菜单对象强制转换为食物。我将按照建议输入界面。
在我的自助餐代码中,在将所有东西添加到菜单对象后调用我的Food方法,我的目标是随机选择要吃的食物,然后返回信息。
我希望我能做一些事情,比如我得到mo =(Food) Menu[rand.Next(Menu.Count)];
的地方,这会让我很容易做到。
我错了,我可能过于复杂了,因为我本来要返回mo
,但每次我尝试投射它时,它都不起作用。
也许我可以使用枚举器之类的东西,但这非常令人困惑。我想我对自己想要的东西有正确的想法,但用语言表达很困难,所以感谢大家对我的耐心。我希望这能更好地解释它:
我的自助餐类
using System;
using System.Collections.Generic;
namespace IronNinja.Models
{
class Buffet
{
public List<Food> Foods = new List<Food>();
public List<Drink> Dranks = new List<Drink>();
public List<object> Menu = new List<object>();
//constructor
public Buffet()
{
Menu.Add(new Food("Chicken Pizza", 1000, false, true));
Menu.Add(new Food("Buffalo Chicken Pizza", 1000, true, false));
Menu.Add(new Food("Lasagna", 1200, false, true));
Menu.Add(new Food("Garden Salad WSalad dressing", 700, true, false));
Menu.Add(new Food("sour patch kids whole box", 700, false, true));
Menu.Add(new Drink("Rootbeer", 700, false));
Menu.Add(new Drink("Not Your Father's Rootbeer", 900, false));
}
// Add a constructor and Serve method to the Buffet class
public Food Serve()
{
Random rand = new Random();
Food mo = ((Food) Menu[rand.Next(Menu.Count)]);
Console.WriteLine(mo);
return new Food("sour patch kids whole box", 700, false, true);
}
}
}
我的饮料类
using IronNinja.Interfaces;
namespace IronNinja.Models
{
public class Drink : IConsumable
{
public string Name { get; set; }
public int Calories { get; set; }
public bool IsSpicy { get; set; }
public bool IsSweet { get; set; }
// Implement a GetInfo Method
public string GetInfo()
{
return $"{Name} (Drink). Calories: {Calories}. Spicy?: {IsSpicy},
Sweet?: {IsSweet.Equals(true)}";
}
// Add a constructor method
public Drink(string name, int calories, bool spicy)
{
Name = name;
Calories = calories;
IsSpicy = spicy;
IsSweet = true;
}
}
}
我的美食级
using IronNinja.Interfaces;
namespace IronNinja.Models
{
class Food : IConsumable
{
public string Name { get; set; }
public int Calories { get; set; }
public bool IsSpicy { get; set; }
public bool IsSweet { get; set; }
public string GetInfo()
{
return $"{Name} (Food). Calories: {Calories}. Spicy?: {IsSpicy},
Sweet?: {IsSweet}";
}
public Food(string name, int calories, bool spicy, bool sweet)
{
Name = name;
Calories = calories;
IsSpicy = spicy;
IsSweet = sweet;
}
}
}
更新
如果你只想随机获取一个食物,你可以使用Linq表达式来获取Food
类型的所有食物。
Random rand = new Random();
var foodItems = Menu.OfType<Food>().ToList();
var randomFood = foodItems[rand.Next(foodItems.Count)];
我会创建一个名为IMenuItem
的接口,它为菜单上的每种类型都具有共享属性。
public interface IMenuItem
{
string Name { get; }
}
然后将该接口添加到Food
和Drink
类中
public class Food : IMenuItem
{
public Food(string name, int calories, bool isSpicy, bool isSweet)
{
Name = name;
Calories = calories;
IsSpicy = isSpicy;
IsSweet = isSweet;
}
public string Name { get; }
public int Calories { get; }
public bool IsSpicy { get; }
public bool IsSweet { get; }
}
public class Drink: IMenuItem
{
public Drink(string name, int calories, bool isSpicy)
{
Name = name;
Calories = calories;
IsSpicy = isSpicy;
}
public string Name { get; }
public int Calories { get; }
public bool IsSpicy { get; }
}
现在,您可以拥有一个菜单项列表,并且可以访问这些共享属性,而无需强制转换任何内容。
var menu = new List<IMenuItem>();
menu.Add(new Food("pizza", calories: 300, isSpicy: false, isSweet: true));
menu.Add(new Drink ("coke", calories: 300, isSpicy: false));
foreach(var menuItem in menu)
{
Console.WriteLine(item.Name);
}
输出
pizza
coke
此外,您可以使用模式匹配来根据它的实际类型进行操作
var menu = new List<IMenuItem>();
menu.Add(new Food("pizza", calories: 300, isSpicy: false, isSweet: true));
menu.Add(new Drink("coke", calories: 300, isSpicy: false));
// get random menu item;
var rand = new Random();
var menuItem = menu[rand.Next(menu.Count)];
switch (menuItem)
{
case Food food: // random item is a food
Console.WriteLine($"Food Name:{food.Name}, Calories:{food.Calories}, IsSpicy:{food.IsSpicy}, IsSweet:{food.IsSweet}");
break;
case Drink drink: // random item is a drink
Console.WriteLine($"Drink Name:{drink.Name}, Calories:{drink.Calories}, IsSpicy:{drink.IsSpicy}");
break;
default: // random item is something else
Console.WriteLine($"Menu Item Name:{menuItem.Name}");
break;
}