zoukankan      html  css  js  c++  java
  • DictionaryBase

    基类DictionaryBase 实现了 IEnumerable和ICollection 。IList接口 IDictionary接口都有add()、remove()方法。

    话不多说直接上案例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace DictionaryBase_Test
    
    {
        public abstract class Animal
        {
            
            public string Name { get; set; }
            public Animal() => Name = "The animal with no name";
            public Animal(string newName) => Name = newName;
            public void Feed() => Console.WriteLine($"{Name} 在吃东西!");
            
        }
    
        public class Animals:DictionaryBase
        {
            public void Add(string newID, Animal newAnimal) =>
                Dictionary.Add(newID, newAnimal);
            public void Remove(string animalID) =>
                Dictionary.Remove(animalID);
            public Animals() { }
            public Animal this[string animalID]
            {
                get { return (Animal)Dictionary[animalID]; }
                set { Dictionary[animalID] = value; }
    
            }
        }
    
        public class Cow:Animal
        {
            public Cow(string newName) : base(newName) { }
            public void Milk()
            {
    
                Console.WriteLine($"{this.Name} 在被挤奶!");
            }
        }
    
        public class Chick:Animal
        {
            public Chick(string newName) : base(newName) { }
            public void LayEgg()
            {
                Console.WriteLine($"{this.Name}在下蛋!");
    
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Animals animalsDictionary = new Animals();
                animalsDictionary.Add("1", new Cow("晖晖"));
                animalsDictionary.Add("2", new Chick("昕昕"));
    
                foreach(DictionaryEntry myEntry in animalsDictionary )
                {
                    Console.WriteLine($"KEY:{myEntry.Key} VALUE:{((Animal)myEntry.Value).Name} ");
                    //Console.WriteLine(myEntry.Value.ToString());
                        
                }
    
                animalsDictionary["1"].Feed();
                ((Chick)animalsDictionary["2"]).LayEgg();
    
                Console.ReadKey();
    
    
            }
        }
    }
  • 相关阅读:
    我的浏览器和常用扩展
    Win10安装.Net Framework4.7及更高版本
    压缩和解压工具bandizip
    Oracle trunc()函数的用法
    Oracle 中 decode 函数用法
    Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法
    String literal is not properly closed by a double-quote eclipse
    linux 启动 Oracle 实例
    查询当前Oracle数据库的实例
    sqlplus 执行 sql 文件
  • 原文地址:https://www.cnblogs.com/Mr-Prince/p/12179945.html
Copyright © 2011-2022 走看看