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(); } } }
查看全文
相关阅读:
[leetcode]Copy List with Random Pointer @ Python
[leetcode]Convert Sorted List to Binary Search Tree @ Python
[leetcode]Convert Sorted Array to Binary Search Tree @ Python
[leetcode]Binary Tree Level Order Traversal II @ Python
[leetcode]Minimum Depth of Binary Tree @ Python
[leetcode]Binary Tree Zigzag Level Order Traversal @ Python
[leetcode]Binary Tree Level Order Traversal @ Python
[leetcode]Sum Root to Leaf Numbers @ Python
[leetcode]Flatten Binary Tree to Linked List @ Python
[leetcode]Binary Tree Postorder Traversal @ Python
原文地址:https://www.cnblogs.com/zhangqs008/p/2398688.html
最新文章
JQuery中each()的使用方法说明
js中的事件委托
移动web开发实践-css3(1)盒模型display:-webkit-box;的使用
HTML5的classList API优化对样式名className的操作
原生JS操作iframe里的dom
未知高度的图片在div设置垂直居中
jquery 实现iframe 自适应高度
$.jsonp()的简单使用
Understanding the WPF Layout System
添加到界面前获取尺寸
热门文章
年金终值公式的推导
C#中==与Equals的区别
C#资源文件与与资源名称字符串之间的互相转化
仅在TabControl中的Tab中添加右键菜单
Path形状获取字符串型变量数据
DependencyProperties or INotifyPropertyChanged ?
Optimizing Performance: Data Binding(zz)
DrawingContext.Pop Method
[leetcode]Binary Tree Maximum Path Sum @ Python
[leetcode]Populating Next Right Pointers in Each Node II @ Python
Copyright © 2011-2022 走看看