Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 制作控制台应用程序游戏的初学者,有财产继承问题_C#_Console Application - Fatal编程技术网

C# 制作控制台应用程序游戏的初学者,有财产继承问题

C# 制作控制台应用程序游戏的初学者,有财产继承问题,c#,console-application,C#,Console Application,在我的输入类中,我有一个switch语句,它过滤所有可能的输入命令。其中一个是“治疗”,我希望它能显示玩家拥有的所有治疗物品,并让他们选择一个来使用。然而,当我达到了玩家治疗的程度,达到了给定的治疗量时,治疗量没有“AmountToHeal”的定义。我有一个名为Item的类,HealItem是一个派生类。当我键入“playerHealth+=healItem”时,仅给出项的属性 case "Heal": if (player.Inventory.OfType<

在我的输入类中,我有一个switch语句,它过滤所有可能的输入命令。其中一个是“治疗”,我希望它能显示玩家拥有的所有治疗物品,并让他们选择一个来使用。然而,当我达到了玩家治疗的程度,达到了给定的治疗量时,治疗量没有“AmountToHeal”的定义。我有一个名为Item的类,HealItem是一个派生类。当我键入“playerHealth+=healItem”时,仅给出项的属性

case "Heal":
                if (player.Inventory.OfType<HealItem>().Any())
                {
                    foreach (var item in player.Inventory)
                    {
                        if (item.GetType() == typeof(HealItem))
                        {
                            Console.WriteLine("You have " + item.Name);
                        }
                    }

                    Console.WriteLine("Which heal item would you like to use? Enter heal item name:");
                    var healItemInput = Console.ReadLine();
                    foreach (var item in player.Inventory)
                    {
                        if (item.GetType() == typeof(HealItem) && healItemInput == item.Name)
                        {

                        }
                    }
                }
HealItem类:

    public class HealItem : Item
{
    public int HealAmount { get; set; }

    public HealItem(string name, string description, int healAmount)
    {
        this.Name = name;
        this.Description = description;
        this.HealAmount = healAmount;
    }
}

我不确定我是否完全理解你的问题,但我意识到你说过,当我键入“playerHealth+=healItem”时,只给出物品的属性。把这个问题当作,为什么不显示HealItem属性,您是否尝试过对HealItem进行显式转换

代码将如下所示:

playerHealth += ((HealItem)item).HealAmount
为了提高可读性,您可以编写:

HealItem healItem = item
playerHealth += healItem.HealAmount

使用HealItem而不是var应该显式地将其转换为HealItem类型。

找到了解决方案!我创建了一个HealItem类型的临时列表,并对该列表进行了迭代,以选择heal项。工作完美无瑕

HealItem healItem = item
playerHealth += healItem.HealAmount