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(); } } }
查看全文
相关阅读:
java8关于list的操作
jdk8的.toMap()
Mybatis中报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题解决
springboot集成bootstrap遇到的问题
java文件下载
idea中mybatis自动生成代码方式
文件读写操作inputStream转为byte[] , 将InputStream写入本地文件
Polar transformer networks
Fine-Grained Person Re-identification
Person Re-identification by Contour Sketch under Moderate Clothing Change
原文地址:https://www.cnblogs.com/zhangqs008/p/2398688.html
最新文章
直播预告丨60分钟带你玩转CTF—SQL注入!
MySQL数据库”mysql SQL Error:1146,SQLState:42S02 “解决方法
HTTP 400 Bad request 原因
ORA-00917: 缺失逗号 Oracle插入数据
关于oracle sql语句查询时表名和字段名要加双引号的问题
java.sql.SQLSyntaxErrorException: ORA-00932: 数据类型不一致: 应为 BINARY, 但却获得 CHAR
centos8 设置 redis 开机自启动
entity, model, domain含义及用途
海致面试
SpringMVC的Controller是单例还是多例?
热门文章
JDK1.8中遍历List集合的几种方式
springboot创建聚合父工程及多个子模块
@RestControllerAdvice自定义异常
SpringBoot整合minio快速入门
mysql中order by rand() limit 1替代方案
java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required
图的三种存储结构
java8关于list的操作(::)
@PostConstruct
Linux修改文件读写权限
Copyright © 2011-2022 走看看