范范(10)

class BaseNode{}
class BaseNodeGeneric<T>{}

//concrete type
class NodeConcrete<T> : BaseNode{}

//closed constructed type
class NodeClosed<T> : BaseNodeGeneric<int>{}

//opend constructed type
class NodeOpen<T> : BaseNodeGeneric<T>{}



 多态是动态的,泛型是静态的。

默认的是这个。

T tmp = default(T);

c# generic 没有c++ template 那么灵活,但是安全。

作业:自己实现一个容器类型,需要有构造、add、remove、clear、[]等方法。

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

namespace ConsoleApplication4
{


    public class GenericList<T>
    {
        private T[] array;
        private int length;
        private int size;

        public GenericList(int length){
            this.length = length;       //length是分配的长度(这里后面几乎没有再使用过length,也没有在增加的时候更改过length,其实是不对的)
            this.size = 0;              //size是实际有内容的长度
            this.array = new T[length];         
       }

        public T add(T data)
        {
            this.array[size++] = data;
            return data;
        }

        public bool remove(T data)
        {
            for(int i = 0;i < size; i++)
            {
                if (data.Equals(this.array[i]))
                {
                    for(int j  = i;j< this.size; j++)
                    {
                        array[j] = array[j + 1];
                    }
                    size--;
                    return true;
                }
            }
            return false;
        }


        public bool clear()
        {
            if (size > 0)
            {
                for (int i = 0; i < this.size; i++)
                {
                    array[i] = default(T);       //使用默认的值
                }
                size = 0;
                return true;
            }
            else
                return false;
        }

        public T this[int location]
        {
            get
            {
                return array[location];
            }
        }


    }
    class Program
    {
      

        static void Main(string[] args)
        {

                GenericList <String> randme= new GenericList<String>(10);
                randme.add("littlepig");
                randme.add("smallduck");
         //   Console.WriteLine(randme.size);

            ///   Console.WriteLine(randme.remove("smallduck"));
            randme.clear();
          //  Console.WriteLine(randme.size);
            Console.WriteLine(randme[1]);
            Console.ReadLine();
        }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值