·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 泛型类的认知

泛型类的认知

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

泛型类的认知

  根据我的理解,什么是泛型?泛型就是一种开放型类型,只有到编译的时候才能确定它的类型,不像传统的封闭类型;根据百度百科解释,即为泛型是程序设计语言的一种特性。允许程序员在强类型程序设计语言中编写代码时定义一些可变部分,那些部分在使用前必须作出指明。各种程序设计语言和其编译器、运行环境对泛型的支持均不一样。将类型参数化以达到代码复用提高软件开发工作效率的一种数据类型。泛型类是引用类型,是堆对象,主要是引入了类型参数这个概念。为了加深理解,我会以一个自定义集合来讲解泛型:

  如果要定义一个自定义集合,无非是用一个类来模拟,类里面的核心属性就是数组,其他还有add、remove、索引器等成员,废话不说,直接上代码
 1 public class IntList 2 { 3     int[] arr = new int[2]; 4     int index = 0; 5     public int count = 0; 6     public void Add(int num) 7     { 8         if (index >= arr.Length) 9         {10             int[] tempArr = new int[arr.Length * 2];11             arr.CopyTo(tempArr, 0);12             arr = tempArr;13         }14         arr[index] = num;15         index++;16         count++;17     }18     public int this[int index]19     {20         get21         {22             if (index < arr.Length)23             {24                 return arr[index];25             }26             else27             {28                 throw new Exception("数组越界");29             }30         }31         set32         {33             if (index < arr.Length)34             {35                 arr[index] = value;36             }37             else38             {39                 throw new Exception("赋值索引越界");40             }41         }42     }43 }
View Code

  从代码中,大家可以看出,在add方法中,当索引长度大于等于数组长度时,数组就会扩容,实质上是实例化一个新数组,然后把元数组数据拷贝到新数组中,再把新数组的引用赋给原数组,我们可以查看一下实际运行结果

1  IntList intlist = new IntList();2  intlist.Add(2);3  intlist.Add(3);4  intlist.Add(4);5  intlist.Add(5);6  Console.WriteLine(intlist.count);
View Code

  这时候,我们算基本完成了自定义集合的功能,等等,这只是int类型的集合,那么,如果想定义string或者其他类型的集合功能,我们又应该怎么办呢,难道每一种类型都要定义一个类吗,这样做其实本身是可以的,可是我们的身为程序猿的尊严不能丢弃,于是这时候一个救星出现了,它就叫做泛型类,上代码:

 1 public class MyList<T> 2 { 3     T[] arr = new T[2]; 4     int index = 0; 5     public int count = 0; 6     public void Add(T num) 7     { 8         if (index >= arr.Length) 9         {10             T[] tempArr = new T[arr.Length * 2];11             arr.CopyTo(tempArr, 0);12             arr = tempArr;13         }14         arr[index] = num;15         index++;16         count++;17     }18     public T this[int index]19     {20         get21         {22             if (index < arr.Length)23             {24                 return arr[index];25             }26             else27             {28                 throw new Exception("数组越界");29             }30         }31         set32         {33             if (index < arr.Length)34             {35                 arr[index] = value;36             }37             else38             {39                 throw new Exception("赋值索引越界");40             }41         }42     }43 }
View Code

  乍一看,这段代码貌似和第一段代码没什么不同,仔细一看,发现int类型不见了,转而是用了一个字母T来代替int类型,还有,貌似定义类的时候多了那么一点点东西。public class MyList<T>,这个T实际上就是一个类型占位符,你可以用任意字符来代替它,但习惯上,都会用T来占位,这样,我们大体上就大功告成了,来看下运行结果

1 MyList<string> mlist = new MyList<string>();2 mlist.Add("1");3 mlist.Add("2m");
View Code

  嗯,貌似大体上功能都搞定了,但是真的结束了吗?答案是No!  我们今天主要是探讨一下泛型类的使用方法,而不仅仅是会用,下边我将用代码的形式为大家拓展一下,没错,下边要讲的就是泛型类型限定,先定义出来我们所需要的类:
1 public interface ipig2 {3 }4 public class Pig : IPig5 {6 }7 public class SmallPig : Pig8 {9 }
View Code
  • T只能为值类型
    1 public class PigList1<T> where T : struct  //泛型类型只能为值类型2 {3 }
    View Code
  • T只能为引用类型
    1 public class PigList2<T> where T : class  //泛型类型只能为引用类型2 {3 }
    View Code
  • T只能为指定类型或者指定类型的子类
    1 public class PigList3<T> where T : Pig  //泛型类型只能为指定类型或者指定类型的子类2 {3 }
    View Code
  • T只能为实现指定接口的类
    1 public class PigList4<T> where T : IPig  //泛型类型只能为实现指定接口的类,或者实现指定接口的子类2 {3 }
    View Code
这样,我们对泛型类也有个基本的认识了,希望大家能互相探讨,本人经验所限,难免班门弄斧,希望各位斧正!