泛型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | // 泛型类------可以一个类型满足不同类型的需求---尖括号+占位符 public class ChildClass2: GenericAbstractClass< int > { //可以已经指定为int类型 } public class ChildClass : GenericAbstractClass<T> { //不行?子类不知道父类是什么类型,延迟声明 } public class ChildClass1<S> : GenericAbstractClass<S> { //可以--子类由父类决定 } public class ChildClass2<S> { public S Show() { // return new S(); //变量没有约束无法创建实例 S没指定 return default (S); //可以 ,返回的是类型 } public S Show2(S s) { return s; //可以,返回的是参数 } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /// <summary> /// 泛型方法 泛型方法----可以一个方法满足不同类型的需求 /// </summary> /// <typeparam name="T">类型变量 类型参数</typeparam> /// <param name="tParameter"></param> public static void Show<T>(T tParameter) { Console.WriteLine($ "This is {typeof(CommonMethod).Name},parameter={tParameter.GetType().Name},type={tParameter}" ); } /// <summary> /// 普通方法 /// </summary> /// <typeparam name="T">类型变量 类型参数</typeparam> /// <param name="tParameter"></param> public static void Show(T tParameter) { //这样和普通方法一样了,括号里面前面是参数类型,后面是参数 所以在前面加<T> 表明这玩意是泛型 Console.WriteLine($ "This is {typeof(CommonMethod).Name},parameter={tParameter.GetType().Name},type={tParameter}" ); } |
1 2 3 4 5 6 7 8 9 10 11 | //interface public interface GenericInterfac<T> { public T Show(); } GenericInterfac< string > sgenericInterfac = null ; GenericInterfac<DateTime> dtgenericInterfac = null ; //delegate public delegate T Genericdelegate<T>(); Genericdelegate< string > sGenericdelegate = null ; Genericdelegate<DateTime> dtGenericdelegate = null ; |
//泛型约束--要有真正的自由--就必须要有约束
//1.基类约束
// a.就是把类型参数当做People
// b.调用---就可以传递Popple或者People的子类型
// c.泛型约束:要么不让你进来,如果让你进来,就一定是没有问题
//2接口约束:
// a.把这个T 当做ISports
// b.就只能传递ISports 这个接口或者是实现过这个接口的类
// c.就可以增加功能,可以获取新的功能
//3引用类型约束
// a.就只能传递类型进来
//4.值类型约束
// a.就只能传递值类型进来
//5.无参数构造函数约束
// b.必须有一个无参数构造函数才能当做参数传入
//6.枚举约束
// a.必须是一个枚举类型才能传入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /// <summary> /// 1.<strong>基类约束</strong> /// a.就是把类型参数People 传入 限定现在的T的类型 /// b.调用---就可以传递Popple或者People的子类型 /// c.泛型约束:要么不让你进来,如果让你进来,就一定是没有问题 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tParameter"></param> public static void ShowBase<T>(T tParameter) where T : <strong>People</strong> { Console.WriteLine($ "People.Id={tParameter.Id}" ); Console.WriteLine($ "People.Name={tParameter.Name}" ); //tParameter.Pingpang(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static void ShowObject( object oParameter) { //Console.WriteLine("This is {0},parameter={1},type={2}", // typeof(GenericConstraint), oParameter.GetType().Name, oParameter); //传递一个实体对象:操作字段和数据 //问题: //1.无法去属性字段--因为oParameter是Object; C#是强类型语言,编译时决定参数是什么类型; //2.强制转换 //Console.WriteLine($"People.Id={oParameter.Id}"); //Console.WriteLine($"People.Name={oParameter.Name}"); People people = (People)oParameter; Console.WriteLine($ "People.Id={people.Id}" ); Console.WriteLine($ "People.Name={people.bing}" ); } // oParameter 传入的不一定是people的实现,转换出问题,但是编译通过的,这个就是<strong>类型安全问题 </strong> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /// <summary> /// <strong>接口约束</strong>: 2 /// a.把这个T 当做ISports /// b.就只能传递ISports 这个接口或者是实现过这个接口的类 /// c.就可以增加功能,可以获取新的功能 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tParameter"></param> public static void ShowInterface<T>(T tParameter) where T : ISports { tParameter.Pingpang(); } //接口约束-- { //GenericConstraint.ShowInterface<People>(people); GenericConstraint.ShowInterface<Chinese>(chinese); GenericConstraint.ShowInterface<Japanese>(japanese); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /// <summary> /// <strong>引用类型约束 3</strong> /// T就只能传递类型进来 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tParameter"></param> public static void ShowClass<T>(T tParameter) where T : class { //T t = new T(); //因为T 可能没有无参数构造构造函数 } { GenericConstraint.ShowClass<People>(people); //GenericConstraint.ShowClass<int>(iValue); //因为Int 是结构--值类型 } /// <summary> /// <strong>值类型约束 4</strong> /// 就只能传递值类型进来 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tParameter"></param> public static void ShowStruct<T>(T tParameter) where T : struct { T t = new T(); } //值类型约束 { //GenericConstraint.ShowStruct<Chinese>(chinese);//应用类型--不行 GenericConstraint.ShowStruct< int >(iValue); //因为Int 是Struct GenericConstraint.ShowStruct<DateTime>(dtValue); //因为DateTime 是Struct } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /// <summary> /// <strong>无参数构造函数约束</strong> /// a. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tParameter"></param> public static void ShowNew<T>(T tParameter) where T : new () { T t = new T(); //明确为值 可以直接实例化 <strong>必须有一个无参数构造函数才能当做参数传入</strong> } //无参数构造函数约束 { GenericConstraint.ShowNew<People>(people); } |
1 2 3 4 5 6 7 8 9 10 | /// <summary> ///<strong> 枚举约束</strong> /// a.必须是个枚举才能够传递进来 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tParameter"></param> public static void ShowEnum<T>(T tParameter) where T : Enum { } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!