zoukankan      html  css  js  c++  java
  • 泛型(一)

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

    namespace 泛型_一__什么是泛型
    {
        public class GenericList<T>
        {
            private class Node
            {
                //字段
                private Node next;
                private T data;

                //有参构造函数
                public Node(T t)
                {
                    next = null;
                    data = t;
                }

                //属性 next
                public Node Next
                {
                    get
                    {
                        return next;
                    }
                    set
                    {
                        next = value;
                    }
                }
                public T Data
                {
                    get
                    {
                        return data;
                    }
                    set
                    {
                        data = value;
                    }
                }    
            }

            //字段
            private Node head;

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

            //方法1
            //给Node添加元素
            public void AddHead(T t)
            {

                Node n = new GenericList<T>.Node(t);
                n.Next = head;
                head = n;
            }

            //方法2
            //IEnumerator<T> 接口,支持在泛型集合上进行简单的迭代
            //yield return 语句返回集合的一个元素,并移动到下一个元素上. yield break可停止迭代.
            public IEnumerator<T> GetEnumerator()
            {
                Node current = head;
                while (current !=null )
                {
                    yield return current.Data;
                    current = current.Next;
                }
            }
        }

      


        class Program
        {
            static void Main(string[] args)
            {
                //GenericList<T> 定义泛型类的对象list,并给予模板参数T为int型
                GenericList<int> list = new GenericList<int>();

                for (int x = 0; x < 10; x++)
                {
                    list.AddHead(x);
                }

                foreach (int i in list)
                {
                    System.Console.WriteLine("  " + i);
                }
                Console.WriteLine("/nDone/n");
                Console.ReadKey();


                GenericList<string> stringList = new GenericList<string>();
                for (int x = 0; x < 10; x++)
                {
                    stringList.AddHead(x.ToString()+"****");
                }
                foreach (string str in stringList)
                {
                    Console.WriteLine("  " + str);
                }
                Console.WriteLine("/nDone/n");
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    【HDOJ6701】Make Rounddog Happy(启发式合并)
    【HDOJ6731】Angle Beats(极角排序)
    【BZOJ1132】Tro(叉积)
    【CF1236D】Alice and the Doll(set)
    Storm
    Spark
    Python基础(2)
    数据库漫谈
    Python基础(1)
    C/C++链接过程相关
  • 原文地址:https://www.cnblogs.com/cpcpc/p/2123127.html
Copyright © 2011-2022 走看看