C#计算自定义类数组中的自定义类对象

时间:2017-06-10 08:12:25

标签: c#

我是c#和编程的新手。 我有一个创建课程的任务"动物"这是" Predator"的父母。和" Herbivorous"而这两个人是父母,#Wolf;#34;," Fox"," Hare"," Deer"分别。所以我做到了。下一步是创建这些对象的数组,并计算阵列中有多少食肉动物和食草动物。这是问题发生的地方。 (类的功能可以是任何)

我的课程:

public abstract class Animal
{

}

public abstract class Predator : Animal
{
    protected string predatorName;
    public Predator (string typeOfAnimal)
    {
        this.predatorName = typeOfAnimal;
    }
}

public abstract class Herbivorous : Animal
{
    protected string herbivorousName;
    public Herbivorous (string typeOfAnimal)
    {
        this.herbivorousName = typeOfAnimal;
    }
}

public class Wolf : Predator
{
    protected string wolfName;
    public Wolf (string typeOfAnimal, string nameOfTheWolf) : base(typeOfAnimal)
    {
        this.wolfName = nameOfTheWolf;
    }
}

public class Fox : Predator
{
    protected string foxName;
    public Fox(string typeOfAnimal, string nameOfTheFox) : base(typeOfAnimal)
    {
        this.foxName = nameOfTheFox;
    }
}

public class Hare : Herbivorous
{
    protected string hareName;
    public Hare(string typeOfAnimal, string nameOfTheHare) : base(typeOfAnimal)
    {
        this.hareName = nameOfTheHare;
    }
}

public class Deer : Herbivorous
{
    protected string deerName;
    public Deer(string typeOfAnimal, string nameOfTheDeer) : base(typeOfAnimal)
    {
        this.deerName = nameOfTheDeer;
    }
}

我的主要人物:

class Program
{
        static void Main(string[] args)
        {                
            Problem2.Wolf wolf1 = new Problem2.Wolf("Predator", "Ghost");
            Problem2.Wolf wolf2 = new Problem2.Wolf("Predator", "Nymeria");
            Problem2.Wolf wolf3 = new Problem2.Wolf("Predator", "Grey Wind");

            Problem2.Fox fox1 = new Problem2.Fox("Predator", "Eevee");
            Problem2.Fox fox2 = new Problem2.Fox("Predator", "Vulpix");

            Problem2.Hare hare1 = new Problem2.Hare("Herbivorous", "Bugs Bunny");
            Problem2.Hare hare2 = new Problem2.Hare("Herbivorous", "Easter Bunny");

            Problem2.Deer deer1 = new Problem2.Deer("Herbivorous", "Bambi");
            Problem2.Deer deer2 = new Problem2.Deer("Herbivorous", "Faline");
            Problem2.Deer deer3 = new Problem2.Deer("Herbivorous", "Sven");
            Problem2.Deer deer4 = new Problem2.Deer("Herbivorous", "Mena");

            Problem2.Animal[] arrayOfAnimals = new Problem2.Animal[] {wolf1, wolf2, wolf3, fox1, fox2, deer1, deer2, deer3, deer4, hare1, hare2};




            for (int i = 0; i < arrayOfAnimals.Length; i++)
            {
                int counter = 0;
                bool check = false;

                Problem2.Herbivorous myHerbivorousAnimal = arrayOfAnimals[i];
                check = myHerbivorousAnimal is Problem2.Herbivorous;

                if (check == true)
                {
                    counter++;
                }
            }
        }
    }`

我有一个编译问题:

  

错误CS0266无法隐式转换类型&#39; Problem2.Animal&#39;至   &#39; Problem2.Herbivorous&#39 ;.存在显式转换(您是否遗漏了   演员?)

出了什么问题?或者是否有正确的方法来计算我的&#34;掠夺者&#34;和&#34; Herbivorous&#34;在我的&#34;动物&#34;? THX

1 个答案:

答案 0 :(得分:1)

Problem2.Herbivorous myHerbivorousAnimal = arrayOfAnimals[i];

您正在尝试将Animal投射到Herbivoros,但不是每个Animal都投放Herbivoros。您可以对当前数组条目执行is检查:

 check =  arrayOfAnimals[i] is Problem2.Herbivorous;
顺便说一句,我不会使用额外的布尔,只需使用if

if (arrayOfAnimals[i] is Problem2.Herbivorous)
{
   counter++;
}

你喊出来让你摆脱for循环,否则它不计算任何东西:

int counter = 0;
for (int i = 0; i < arrayOfAnimals.Length; i++)
{
    if (arrayOfAnimals[i] is Problem2.Herbivorous)
    {
       counter++;
    }
}