泛型接口

跟風遠走 提交于 2020-03-05 20:05:19

   不论是为泛型容器类,还是表示容器中元素的泛型类,定义接口是很有用的。把泛型接口与泛型类结合使用是更好的用法,比如用IComparable<T>而非IComparable,以避免值类型上的装箱和拆箱操作。.NET框架2.0类库定义了几个新的泛型接口,以配合System.Collections.Generic中新容器类的使用。

    当一个接口被指定为类型参数的约束时,只有实现该接口的类型可被用作类型参数。下面的示例代码显示了一个从MyList<T>派生的SortedList<T>类。更多信息,请参见泛型概述。SortedList<T>增加了约束where T : IComparable<T>。

这使得SortedList<T>中的BubbleSort方法可以使用表中的元素的IComparable<T>.CompareTo方法。在这个例子中,表中的元素是简单类——实现IComparable<Person>的Person类。

using System;
using System.Collections.Generic;
 
//Type parameter T in angle brackets.
public class MyList<T>
{
    protected Node head;
    protected Node current = null;
 
// Nested type is also generic on T
    protected class Node         
    {
        public Node next;
//T as private member datatype.
        private T data;         
//T used in non-generic constructor.
        public Node(T t)        
        {
            next = null;
            data = t;
        }
        public Node Next
        {
            get { return next; }
            set { next = value; }
        }
//T as return type of property.
        public T Data           
        {
            get { return data; }
            set { data = value; }
        }
    }
    public MyList()
    {
        head = null;
    }
//T as method parameter type.
    public void AddHead(T t)    
    {
        Node n = new Node(t);
        n.Next = head;
        head = n;   
    }
    // Implement IEnumerator<T> to enable foreach
    // iteration of our list. Note that in C# 2.0
    // you are not required to implment Current and
    // GetNext. The compiler does that for you.
    public IEnumerator<T> GetEnumerator()
    {
        Node current = head;
 
        while (current != null)
        {
            yield return current.Data;
            current = current.Next;
        }
    }
}
 
 
public class SortedList<T> : MyList<T> where T : IComparable<T>
{
    // A simple, unoptimized sort algorithm that
    // orders list elements from lowest to highest:
 
public void BubbleSort()
    {
 
        if (null == head || null == head.Next)
            return;
        bool swapped;
 
        do
        {
            Node previous = null;
            Node current = head;
            swapped = false;
 
            while (current.next != null)
            {
                //  Because we need to call this method, the SortedList
                //  class is constrained on IEnumerable<T>
                if (current.Data.CompareTo(current.next.Data) > 0)
                {
                    Node tmp = current.next;
                    current.next = current.next.next;
                    tmp.next = current;
 
                    if (previous == null)
                    {
                        head = tmp;
                    }
                    else
                    {
                        previous.next = tmp;
                    }
                    previous = tmp;
                    swapped = true;
                }
 
                else
                {
                    previous = current;
                    current = current.next;
                }
 
            }// end while
        } while (swapped);
    }
 
}
 
// A simple class that implements IComparable<T>
// using itself as the type argument. This is a
// common design pattern in objects that are
// stored in generic lists.
public class Person : IComparable<Person>
{
    string name;
    int age;
    public Person(string s, int i)
    {
        name = s;
        age = i;
    }
    // This will cause list elements
    // to be sorted on age values.
    public int CompareTo(Person p)
    {
        return age - p.age;
    }
    public override string ToString()
    {
        return name + ":" + age;
    }
    // Must implement Equals.
    public bool Equals(Person p)
    {
        return (this.age == p.age);
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        //Declare and instantiate a new generic SortedList class.
        //Person is the type argument.
        SortedList<Person> list = new SortedList<Person>();
 
        //Create name and age values to initialize Person objects.
        string[] names = new string[]{"Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul"};
        int[] ages = new int[]{45, 19, 28, 23, 18, 9, 108, 72, 30, 35};
 
        //Populate the list.
        for (int x = 0; x < 10; x++)
        {
            list.AddHead(new Person(names[x], ages[x]));
        }
        //Print out unsorted list.
        foreach (Person p in list)
        {
            Console.WriteLine(p.ToString());
        }
 
        //Sort the list.
        list.BubbleSort();
 
        //Print out sorted list.
        foreach (Person p in list)
        {
            Console.WriteLine(p.ToString());
        }
 
        Console.WriteLine("Done");
    }
}
 
View Code

可以在一个类型指定多个接口作为约束,如下:

class Stack<T> where T : IComparable<T>, IMyStack1<T>{}

一个接口可以定义多个类型参数,如下:

IDictionary<K,V>

接口和类的继承规则相同:

//Okay.
IMyInterface : IBaseInterface<int>
//Okay.
IMyInterface<T> : IBaseInterface<T>
 
//Okay.
IMyInterface<T>: IBaseInterface<int>
//Error.
IMyInterface<T> : IBaseInterface2<T, U>

具体类可以实现封闭构造接口,如下:

class MyClass : IBaseInterface<string>
 
泛型类可以实现泛型接口或封闭构造接口,只要类的参数列表提供了接口需要的所有参数,如下:
//Okay.
class MyClass<T> : IBaseInterface<T>
//Okay.
class MyClass<T> : IBaseInterface<T, string>

泛型类、泛型结构,泛型接口都具有同样方法重载的规则。详细信息,请参见泛型方法。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!