一个非常有用的关于集合的例子,实现集合的add,remove和this[index]方法

本文通过C#语言展示了如何创建一个包含多种动物类别的自定义集合。主要实现了动物基类Animal,并派生出Cow(奶牛)和Chicken(鸡)两个子类,每个子类都具有特定的行为如产蛋和挤奶等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Animals.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   public class Animals : DictionaryBase
   {
      public void Add(string newID, Animal newAnimal)
      {
         Dictionary.Add(newID, newAnimal);
      }

      public void Remove(string animalID)
      {
         Dictionary.Remove(animalID);
      }

      public new IEnumerator GetEnumerator()
      {
         foreach (object animal in Dictionary.Values)
            yield return (Animal)animal;
      }

      public Animal this[string animalID]
      {
         get
         {
            return (Animal)Dictionary[animalID];
         }
         set
         {
            Dictionary[animalID] = value;
         }
      }
   }

}
View Code
program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   class Program
   {
      static void Main(string[] args)
      {
         Animals animalCollection = new Animals();
         animalCollection.Add("Jack", new Cow("Jack"));
         animalCollection.Add("Vera", new Chicken("Vera"));
         foreach (Animal myAnimal in animalCollection)
         {
            Console.WriteLine("New {0} object added to custom collection, " +
                              "Name = {1}", myAnimal.ToString(), myAnimal.Name);
         }
         Console.ReadKey();
      }
   }
}
View Code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   public abstract class Animal
   {
      protected string name;

      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }

      public Animal()
      {
         name = "The animal with no name";
      }

      public Animal(string newName)
      {
         name = newName;
      }

      public void Feed()
      {
         Console.WriteLine("{0} has been fed.", name);
      }
   }
}
View Code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   public class Chicken : Animal
   {
      public void LayEgg()
      {
         Console.WriteLine("{0} has laid an egg.", name);
      }

      public Chicken(string newName) : base(newName)
      {
      }
   }
}
View Code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DictionaryAnimals
{
   public class Cow : Animal
   {
      public void Milk()
      {
         Console.WriteLine("{0} has been milked.", name);
      }

      public Cow(string newName) : base(newName)
      {
      }
   }
}
View Code

 

 

转载于:https://www.cnblogs.com/swtool/p/5524832.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值