个彻底解释 C#泛型的源代码

using System;
using System.Collections.Generic;
using System.Text;

namespace Iinterface
{
    //声明T类型.
    public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
    {
        protected Node head;
        protected Node current = null;

        // 定义一个Node类,该类也是泛型类型
        protected class Node
        {
            public Node next;
            //T类型的成员
            private T data;

            //构造函数不是泛型类型,参数类型应用了T类型
            public Node(T t)
            {
                next = null;
                data = t;
            }

            public Node Next
            {
                get { return next; }
                set { next = value; }
            }

            //属性的返回类型是T类型
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
        }

        //构造函数
        public GenericList()
        {
            head = null;
        }

        //增加元素
        public void AddHead(T t)
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }

        // 迭代
        public System.Collections.Generic.IEnumerator<T> GetEnumerator()
        {
            Node current = head;
            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }


    /// <summary>
    /// 排序
    /// where 子句用于指定类型约束,这些约束可以作为泛型声明中定义的类型参数的变量
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
    {
        // 约束条件
        //冒泡排序
        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)
                {
                    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;
                    }
                }
            } while (swapped);
        }
    }


   
  
    // 使用广州达内IComparable<T>.
    public class Person : System.IComparable<Person>
    {
        string name;
        int age;

        public Person(string s, int i)
        {
            name = s;
            age = i;
        }

        // 实现按照Age排序.
        public int CompareTo(Person p)
        {
            return age - p.age;
        }

        public override string ToString()
        {
            return name + ":" + age;
        }

        // 必须实现Equals操作.
        public bool Equals(Person p)
        {
            return (this.age == p.age);
        }
    }


    /// <summary>
    /// 主函数
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //声明一个新的泛型对象.
            //Person是类型参数.
            SortedList<Person> list = new SortedList<Person>();

            //创建一个Person对象的成员.
            string[] names = new string[]
        {
            "王五",
            "赵燕",
            "李毅",
            "马龙",
            "斯托克顿",
            "克林顿",
            "马晓旭",
            "张望",
            "林炳",
            "钱龙"
        };

            int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };

            //填入成员.
            for (int x = 0; x < 10; x++)
            {
                list.AddHead(new Person(names[x], ages[x]));
            }

            //打印没有排序的成员.
            foreach (Person p in list)
            {
                System.Console.WriteLine(p.ToString());
            }
            System.Console.WriteLine("还没有排序呢!\n");

            //排序.
            list.BubbleSort();

            //打印排序后的成员.
            foreach (Person p in list)
            {
                System.Console.WriteLine(p.ToString());
            }

            System.Console.WriteLine("排序完成啦!");

            Console.Read();
        }
    }
}

posted on 2012-11-23 11:48  java开发学习  阅读(233)  评论(0编辑  收藏  举报

导航