自己动手做一个泛型

1.泛型是一个长度动态变化,类型一致的数组,初始化如下
                                                                                           :  List<T> list=new List<T>();
                                                                                            例如我们要:list.Add(new Student("aa"));

2.首先创建一个Student 类,如下
          public  Student() { }
         //创建带string name 参数的构造函数
         public  Student(  string  name)
        {
             this .Name = name;
        }
         string  name;

         public  string  Name
        {
             get  {  return  name; }
             set  { name =  value ; }
        }
3.创建一个MyList类,在类里面创建一个长度为4的Student数组,类似泛型 List<Student> list=new List<Student>();  同时因为泛型是长度动态变化,增加两个字段count(数组实际元素) ,capacity(容量),做好属性封装,同时创建一个Add方法,往数组添加元素,如下
  //创建一个长度为4的Student数组 :类似泛型的List<Student> list=new List<Student>();
         Student [] stus =  new  Student [4];
       
         int  capacity;
         //数组容量
         public  int  Capacity
        {
             get  {  return  capacity; }
             set  { capacity =  value ; }
        }
         int  count;
         //数组实际元素个数
         public  int  Count
        {
             get  {  return  count; }
             set  { count =  value ; }
        }

        #region  新增 类似泛型的list.Add() + public void Add(Student stu)
         public  void  Add(  Student  stu)
        {
             //首先要判断实际个数是否大于容量,如果是,则增容
             if  (Count > stus.Length)
            {
                 //创建一个新的数组用来复制原来数组的数据
                 Student [] newStus =  new  Student [stus.Length * 2];
                 //复制数组数据
                 Array .Copy(stus, newStus, Count);
                 //将新数组的地址引用复制到旧数组中去
                stus = newStus;
            }
             //将传进来的student stu添加到数组中去
            stus[count] = stu;
             //实际个数++,Capacity=数组长度
            count++;
             this .Capacity = stus.Length;
        }
        #endregion
4.这样在Main那里就可以有    
              MyList  mylist =  new  MyList ();
            mylist.Add(  new  Student (  "aa" ));
            mylist.Add(  new  Student (  "bb" ));
            mylist.Add(  new  Student (  "cc" ));
            mylist.Add(  new  Student (  "dd" ));
            mylist.Add( new  Student ( "test" ));
 但是发现   mylist [4] =  new  Student ( "ee" ); 这个会报错,而泛型这样使用是不会,因此要创建一个索引器,相当于泛型中的赋值和取值;
5.在MyList中创建索引器 
        #region  创建索引器,相当于泛型中的赋值跟取值 public Student this[int index]
         public  Student  this [  int  index]
        {
             //做好判断,比较用户传进来的下标跟stus里面实际长度
             get
            {
                 if  (index>=stus.Length)
                {
                     throw  new  IndexOutOfRangeException ();
                }
                 return  stus[index];
            }
             set
            {
                 if  (index >= stus.Length)
                {
                     throw  new  IndexOutOfRangeException ();
                }
                stus[index]=  value ;
            }
        }
        #endregion
此时发现  mylist [5] =  new  Student ( "ee" ); 并没有报错,至此,差不多完成泛型的定义,添加方法
然后泛型可以用foreach进行遍历输出,但是我们做的是一个数组,无法用foreach输出(因为用foreach要继承IEnumerator这个接口)
提示错误如下:
6.因此我们不妨新建一个 MyEnumerator类  继承 IEnumerator这个接口
    然后右键实现接口,有三个方法 
            public  object  Current   //当前元素  
             public  bool  MoveNext() // 判断是否可以移动到数组的下一个元素
             public  void  Reset()          这个可以不理
        重写上面两个方法,如下:
          Student [] stus;
       
         public  MyEnumerator() { }  //无参构造函数
      
         public  MyEnumerator(  Student  [] stus)  //有参构造函数
        {
             this .stus = stus;  //将MyList的stus传进来
        }
         int  index = -1;  //索引值,用于MoveNext判断是否越界

         public  object  Current  //当前元素
        {
             get  {  return  stus[index]; }
        }

        #region  //判断是否可以移动到数组的下一个元素  public bool MoveNext()
        
         public  bool  MoveNext()
        {
            index++;
             if  (index < stus.Length)
            {
                 return  true ;
            }
             return  false ;
        }
        #endregion

         public  void  Reset()
        {
             throw  new  NotImplementedException ();
        }
7.要在MyList类中 返回一个IEnumerator 类型的变量 其实就是返回一个实现了这个接口的类的对象
代码如下:
  #region  返回IEnumerator类型变量,使MyList实现IEnumerator接口 public IEnumerator GetEnumerator()
         public  IEnumerator  GetEnumerator()
        {
             Student [] newStus =  new  Student [count];
             Array .Copy(stus, newStus, count);
             return  new  MyEnumerator (newStus);
        }
        #endregion
8.此时,可以用foreach来遍历MyList里面的元素了
           foreach (  Student  stu  in  mylist)
            {
                 Console .WriteLine(stu.Name);
            }
9.点击运行,显示成功:
1.泛型是一个长度动态变化,类型一致的数组,初始化如下
                                                                                           :  List<T> list=new List<T>();
                                                                                            例如我们要:list.Add(new Student("aa"));

2.首先创建一个Student 类,如下
          public  Student() { }
         //创建带string name 参数的构造函数
         public  Student(  string  name)
        {
             this .Name = name;
        }
         string  name;

         public  string  Name
        {
             get  {  return  name; }
             set  { name =  value ; }
        }
3.创建一个MyList类,在类里面创建一个长度为4的Student数组,类似泛型 List<Student> list=new List<Student>();  同时因为泛型是长度动态变化,增加两个字段count(数组实际元素) ,capacity(容量),做好属性封装,同时创建一个Add方法,往数组添加元素,如下
  //创建一个长度为4的Student数组 :类似泛型的List<Student> list=new List<Student>();
         Student [] stus =  new  Student [4];
       
         int  capacity;
         //数组容量
         public  int  Capacity
        {
             get  {  return  capacity; }
             set  { capacity =  value ; }
        }
         int  count;
         //数组实际元素个数
         public  int  Count
        {
             get  {  return  count; }
             set  { count =  value ; }
        }

        #region  新增 类似泛型的list.Add() + public void Add(Student stu)
         public  void  Add(  Student  stu)
        {
             //首先要判断实际个数是否大于容量,如果是,则增容
             if  (Count > stus.Length)
            {
                 //创建一个新的数组用来复制原来数组的数据
                 Student [] newStus =  new  Student [stus.Length * 2];
                 //复制数组数据
                 Array .Copy(stus, newStus, Count);
                 //将新数组的地址引用复制到旧数组中去
                stus = newStus;
            }
             //将传进来的student stu添加到数组中去
            stus[count] = stu;
             //实际个数++,Capacity=数组长度
            count++;
             this .Capacity = stus.Length;
        }
        #endregion
4.这样在Main那里就可以有    
              MyList  mylist =  new  MyList ();
            mylist.Add(  new  Student (  "aa" ));
            mylist.Add(  new  Student (  "bb" ));
            mylist.Add(  new  Student (  "cc" ));
            mylist.Add(  new  Student (  "dd" ));
            mylist.Add( new  Student ( "test" ));
 但是发现   mylist [4] =  new  Student ( "ee" ); 这个会报错,而泛型这样使用是不会,因此要创建一个索引器,相当于泛型中的赋值和取值;
5.在MyList中创建索引器 
        #region  创建索引器,相当于泛型中的赋值跟取值 public Student this[int index]
         public  Student  this [  int  index]
        {
             //做好判断,比较用户传进来的下标跟stus里面实际长度
             get
            {
                 if  (index>=stus.Length)
                {
                     throw  new  IndexOutOfRangeException ();
                }
                 return  stus[index];
            }
             set
            {
                 if  (index >= stus.Length)
                {
                     throw  new  IndexOutOfRangeException ();
                }
                stus[index]=  value ;
            }
        }
        #endregion
此时发现  mylist [5] =  new  Student ( "ee" ); 并没有报错,至此,差不多完成泛型的定义,添加方法
然后泛型可以用foreach进行遍历输出,但是我们做的是一个数组,无法用foreach输出(因为用foreach要继承IEnumerator这个接口)
提示错误如下:
6.因此我们不妨新建一个 MyEnumerator类  继承 IEnumerator这个接口
    然后右键实现接口,有三个方法 
            public  object  Current   //当前元素  
             public  bool  MoveNext() // 判断是否可以移动到数组的下一个元素
             public  void  Reset()          这个可以不理
        重写上面两个方法,如下:
          Student [] stus;
       
         public  MyEnumerator() { }  //无参构造函数
      
         public  MyEnumerator(  Student  [] stus)  //有参构造函数
        {
             this .stus = stus;  //将MyList的stus传进来
        }
         int  index = -1;  //索引值,用于MoveNext判断是否越界

         public  object  Current  //当前元素
        {
             get  {  return  stus[index]; }
        }

        #region  //判断是否可以移动到数组的下一个元素  public bool MoveNext()
        
         public  bool  MoveNext()
        {
            index++;
             if  (index < stus.Length)
            {
                 return  true ;
            }
             return  false ;
        }
        #endregion

         public  void  Reset()
        {
             throw  new  NotImplementedException ();
        }
7.要在MyList类中 返回一个IEnumerator 类型的变量 其实就是返回一个实现了这个接口的类的对象
代码如下:
  #region  返回IEnumerator类型变量,使MyList实现IEnumerator接口 public IEnumerator GetEnumerator()
         public  IEnumerator  GetEnumerator()
        {
             Student [] newStus =  new  Student [count];
             Array .Copy(stus, newStus, count);
             return  new  MyEnumerator (newStus);
        }
        #endregion
8.此时,可以用foreach来遍历MyList里面的元素了
           foreach (  Student  stu  in  mylist)
            {
                 Console .WriteLine(stu.Name);
            }
9.点击运行,显示成功:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值