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(); } } }
查看全文
相关阅读:
Why use strong named assemblies?
Dependency Walker
“等一下,我碰!”——常见的2D碰撞检测
MOBA游戏的网络同步技术
VS2017如何配置openGL环境
UE4关于Oculus Rift (VR)开发忠告
UE4 的json读写方式
Blueprint 编译概述
UE4编码规范
Unreal Enginer4特性介绍
原文地址:https://www.cnblogs.com/zhangqs008/p/2398688.html
最新文章
小技巧找出一个php的cron脚本出问题的代码行
ffmpeg使用转码学习
bootbox上的浮层弹出如何加上datepicker
做一个群组聊天页面
javascrpt插入html中中文字符乱码问题记录
Excel 批量快速合并相同的单元格:数据透视表、宏代码、分类汇总
突破技术管理,IT人中年危机变契机
Shell脚本报错unary operator expected
数据仓库的一些建议
delete
热门文章
一次delete速度异常慢的处理过程
mysql求交集:UNION ALL合并查询,inner join内连接查询,IN/EXISTS子查询
MySQL create table as与create table like对比
SQL Change Automation --> Generating migrations and Applying migrations
SQL Change Automation --> Setting up a project with an existing database in SSMS
SQL Change Automation --> Baseline scripts
ssms 生成迁移脚本 Generate script in SQL Server Management Studio
Working with scripts folders【SQL Compare】
Existing database setup
正则密码 Regex and Password complexity policy
Copyright © 2011-2022 走看看