zoukankan
html css js c++ java
C#自定义泛型
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<T> : IEnumerable<T> where T : Car//:下面的泛型集合类的项目必须是Car 或Car的继承类 { private List<T> arCars = new List<T>(); 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<T>扩展自IEnumerable的,因此,我们需要实现的GetEnumerator()方法的两个版本。 System.Collections.Generic.IEnumerator<T> IEnumerable<T>.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 *****\n"); CarCollection<Car> myCars = new CarCollection<Car>(); 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<Car> can hold any type deriving from Car. CarCollection<Car> myAutos = new CarCollection<Car>(); 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(); } } }
查看全文
相关阅读:
在新浪爱问上看到的有趣名字都记下来
FastReports_4.14.1 _Cliff手动安装
计算机算法对做事效率的启发(既要高强度猛攻,也要细水长流)
有趣的数组
listView 分页加载数据
如何隐藏Cognos Viewer
使用DBUnit实现对数据库的测试
通过Jasmine和Guard自动测试JavaScript
for惠普2013实习生
栈的顺序存储,所谓的顺序栈
原文地址:https://www.cnblogs.com/zhangqs008/p/2398688.html
最新文章
数据库面试
Delphi日期设置为NULL
C# 根据路线点集合动态分段
MIS货物拆包销售的问题
JS改变HTML元素的绝对坐标
JS给TR隔行换色,鼠标经过有动感
JS把内容动态插入到DIV
HTTPCLIENT抓取网页内容
JS不改HTML任何代码就达到动态效果
JS取得不同连接的地址,并打开新窗口
热门文章
JS和CSS的初步入门(JS可以取得所有p的内容并显示)
JS当页换图片(分析href所给的信息)
JS让DIV绑定某个事件
hdu 4561 模拟小题
JS全选checkbox
对protected函数的简单理解 good
SystemParametersinfo
正则表达式匹配(全部经过验证)
函数的调用规则——简单记忆
Linux Kbuild工作原理分析(以DVSDK生成PowerVR显卡内核模块为例)
Copyright © 2011-2022 走看看