2017-06-10 45 views
0

我是新来的c#和编程。 我有一个任务是创建一个“动物”类,它是“掠食者”和“草食性”的父类,这两个分别是“狼”,“狐狸”,“野兔”,“鹿”的父母。所以我做到了。下一步是创建一个这些对象的数组,并统计有多少食肉动物和食草动物。这是问题发生的地方。 (类的功能可以任意)C#在自定义类数组中计数自定义类对象

我的课:

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不能隐将类型'Problem2.Animal'转换为 'Problem2.Herbivorous'。存在明确的转换(您是否缺少 个演职员表?)

所以出了什么问题?还是在我的“动物”阵列中有一个正确的方法来计算我的“掠食者”和“草食性”? THX

+0

你可以做到这一点'arrayOfAnimals.OfType ()COUNT();'。这是一个非常简单的方法。这只取决于你是否学习使用基础知识进行编码,或者想用最有效的方式表达你所需要的东西。 – Enigmativity

+0

另一项任务是让该柜台与“is”或“as”一起工作。所以过了一段时间我重写了我的代码,就像这个'var herbivirousCounter = allAnimals.Where(x => x是Problem2.Herbivorous).ToList();' –

回答

1
Problem2.Herbivorous myHerbivorousAnimal = arrayOfAnimals[i]; 

您正在尝试的Animal投进去Herbivoros,但不是每个AnimalHerbivoros。你可以做对当前数组项的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++; 
    } 
} 
+0

thx。我发布了,然后我发现了它。想删除帖子,我看到了你的评论。 THX再次澄清。干杯 –