zoukankan      html  css  js  c++  java
  • C# 中的泛型应用

    泛型在我理解就好比是一个袋子,可以装下任何东西.我们当然不希望用这个袋子又装好吃的又要装垃圾.打一个更形象的比方,去超市买一包饼干,当你结帐的时候服务员会给你一个袋子装这包饼干.但是对于我们来说饼干与饼干的包装袋都装到了这个大袋子里,而饼干是我们所需要的,包装袋子就是垃圾.这时就会引出一个概念,"装箱、拆箱".将包装袋装饼干的过程就叫"装箱",而我们拆包装吃饼干的过程叫"拆箱".显然这对于吃饼干来说是非常浪费时间的.所以我们可能会需要一个可以做自我提示的袋子,也就是说如果你想用这个袋子来装饼干,那么你用它来装糖果,它就会自动提示你,它已经被你指定成装饼干了.

    上面的例子虽然不太贴切,但是对于理解泛型还是有些帮助的.

    大概有以下几个集合类型:

    1. List,这是我们应用最多的泛型种类,它对应ArrayList集合。

    2. Dictionary,这也是我们平时运用比较多的泛型种类,对应Hashtable集合。

    3. Collection对应于CollectionBase

    4. ReadOnlyCollection 对应于ReadOnlyCollectionBase,这是一个只读的集合。

    5. Queue,Stack和SortedList,它们分别对应于与它们同名的非泛型类。

    看一下这个类:

     


     1public class PersonCollection : IList
     2{
     3    private ArrayList _Persons = new ArrayList();
     4    public Person this[int index]
     5    get return (Person) _Persons[index]; } }
     6    public int Add(Person item)
     7    {
     8        _Persons.Add(item);
     9        return _Persons.Count - 1;
    10    }

    11    public void Remove(Person item)
    12    { _Persons.Remove(item); }
    13
    14    object IList.this[int index]
    15    {
    16        get return _Persons[index]; }
    17        set { _Persons[index] = (Person)value; }
    18    }

    19    int IList.Add(object item)
    20    return Add((Person)item); }
    21    void IList.Remove(object item)
    22    { Remove((Person)item); }
    23
    24
    25}

    26
    27

     

    这个类是Person类的操作类,可以自由的增加或删除Person类.如果现在要为其他的类写一个功能与这个类一样的操作类,相信只需要将Person类替换一下就可以了.不过在了解泛型之后你就可以这样来用.

    List<Person> persons = new List<Person>();

    persons.Add(new Person());

    Person person = persons[0]; 

    比如,如果要将Person类换成Employee类,只需要像这样写.

    List<Employee> employees= new List<Employee>();

    employees.Add(new Employee());

    Employee employee= employees[0];

    List是C#中已经定义好的泛型类,现在我们可以自己定义它.

     

    TypeHelper

     

    这里的T只是一个类型的占位符,在实际应用的时候,将实际的类型替换掉T就可以.

    TypeHelper<Person> typeHelper = new TypeHelper();

    typeHelper.GetType(Person);

    注意T只是一个占位符,实际上换上任何符号都可以,千万不要任为只有T可以做占位符.

    有的时候我们必须要约束一下实际的类型,比如以下的泛型类.

     


     1public class CollectionHelper<T,V>
     2
     3{
     4
     5       private T Collec = new T();
     6
     7       public int IndexOf(V v){
     8
     9           return Collec.IndexOf(v);        
    10
    11       }

    12}

    13

     

    泛型类中的T,V显示不是随便什么类型都可以代替的,首先这个类型T必须具有IndexOf方法,V必须是一个引用类型.所以这个类要修改一下.

     


     1public class CollectionHelper<T,V> where T:IList
     2
     3                                   where V:class
     4
     5{
     6
     7       private T Collec = new List();
     8
     9       public int IndexOf(V v){
    10
    11           return Collec.IndexOf(v);        
    12
    13       }

    14
    15}

    16

     

    where 是关键字,T是表示所要进行约束的类型.IList是表示要实现的接口,显示只要实现IList接口,就一定有IndexOf方法,V的约束是必须是一个类.

    如果要求类型必须是一个值类型的参数,就需要使用struct.如果还需要将类型重新实例化就需要使用new()来进行限制,说明该类型必须要有一个无参的构造函数.

    注意如果一个类型需要有多个约束进行限制,那么new()必须写在最后面.

  • 相关阅读:
    正文提取2
    使用mysqlimport导入数据
    batch_get_real_image_urls.py 博源
    用于网页分类的python工具包
    python learning base 不错
    20101010网站更新部署
    20101010网站更新部署
    正文提取
    python property
    jquery slideUp()
  • 原文地址:https://www.cnblogs.com/engine1984/p/1696797.html
Copyright © 2011-2022 走看看