• 泛型


    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace CustomGenericCollection
    {
        #region 汽车的定义
        public class Car
        {
            public string PetName;
            public int Speed;
    
            public Car(string name, int currentSpeed)
            {
                PetName = name;
                Speed = currentSpeed;
            }
            public Car() { }
        }
    
        public class SportsCar : Car
        {
            public SportsCar(string p, int s)
                : base(p, s) { }
            // 其他方法
        }
    
        public class MiniVan : Car
        {
            public MiniVan(string p, int s)
                : base(p, s) { }
            // 其他方法
        }
        #endregion
    
        #region 自定义泛型集合
        public class CarCollection : IEnumerable where T : Car//:下面的泛型集合类的项目必须是Car 或Car的继承类
        {
            private List arCars = new List();
            public T GetCar(int pos)
            {
                return arCars[pos];
            }
            public void AddCar(T c)
            {
                arCars.Add(c);
            }
            public void ClearCars()
            {
                arCars.Clear();
            }
            public int Count
            {
                get { return arCars.Count; }
            }
            // IEnumerable扩展自IEnumerable的,因此,我们需要实现的GetEnumerator()方法的两个版本。
            System.Collections.Generic.IEnumerator IEnumerable.GetEnumerator()
            {
                return arCars.GetEnumerator();
            }
    
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return arCars.GetEnumerator();
            }
    
            public void PrintPetName(int pos)
            {
                Console.WriteLine(arCars[pos].PetName);
            }
        }
        #endregion
    
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("***** Custom Generic Collection *****
    ");
                CarCollection myCars = new CarCollection();
                myCars.AddCar(new Car("Rusty", 20));
                myCars.AddCar(new Car("Zippy", 90));
    
                foreach (Car c in myCars)
                {
                    Console.WriteLine("PetName: {0}, Speed: {1}", c.PetName, c.Speed);
                }
                Console.WriteLine();
    
                // CarCollection can hold any type deriving from Car.
                CarCollection myAutos = new CarCollection();
                myAutos.AddCar(new MiniVan("Family Truckster", 55));
                myAutos.AddCar(new SportsCar("Crusher", 40));
                foreach (Car c in myAutos)
                {
                    Console.WriteLine("Type: {0}, PetName: {1}, Speed: {2}",
                      c.GetType().Name, c.PetName, c.Speed);
                }
    
                Console.ReadLine();
            }
        }
    }
    
  • 相关阅读:
    2020年蓝桥杯校内模拟赛
    kaggle入门——泰坦尼克之灾
    在线程池里面执行
    如何使用在线工具手动验证JWT签名
    python日志模块
    性能测试
    自动生成时间
    jmeter + tomcat + ant + svn +jenkins 实现持续集成测试
    JMeter性能测试,完整入门篇
    jmeter 24个常用函数
  • 原文地址:https://www.cnblogs.com/lonelyofsoul/p/3654713.html
Copyright © 2020-2023  润新知