zoukankan      html  css  js  c++  java
  • 一个非常有用的关于集合的例子,实现集合的add,remove和this[index]方法

    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

  • 相关阅读:
    NOIP2014飞扬的小鸟[DP][WRONG]
    POJ2184 Cow Exhibition[DP 状态负值]
    Quantum Bogo sort浅谈
    POJ3211 Washing Clothes[DP 分解 01背包可行性]
    VIJOS P1426兴奋剂检查[DP 状态哈希]
    VIJOS P1037搭建双塔[DP]
    NOIP2006金明的预算方案[DP 有依赖的背包问题]
    POJ1742 Coins[多重背包可行性]
    NOIP水题合集[3/未完待续]
    单调队列
  • 原文地址:https://www.cnblogs.com/swtool/p/5524832.html
Copyright © 2011-2022 走看看