泛型初识

记得在做机房的时候遇到这种问题。两个函数仅仅是參数类型不一样,其它基本都一样,但是不知道如何做才干降低这种情况。那个时候感觉这个问题挺别扭的,后来听大家都在利用泛型集合,我也就用了,至于为什么用,有什么优点。当时不太理解。今天听了解说,认认真真的把代码实现了,焕然大悟。假设当初我多思考思考就不至于如今才弄懂这个问题。学习就是这样。有时候感觉自己走的挺好的。不愿意研究那似懂的问题,事实上到最后是逃只是的!

如今把之前的课补上吧!

<span style="font-size:18px;">#region Author & Version
/*
**********************************************************************************
 *作者:**
 * 小组:**
 * 说明: **
 *创建日期:2015/7/29 16:17:01
 * 版本:V3.1.0
*********************************************************************************
*/
#endregion

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

namespace ITOO.Generic
{
    public class SortHelper
    { 
     //參数为int数组的冒泡排序
        public void BubbleSort(int[] array)
        {
            int length = array.Length;
            for (int i = 0; i <= length - 2; i++)
            {
                for (int j = length - 1; j >= 1; j--)
                {
                    //对两个元素进行交换
                    if (array[j] < array[j - 1])
                    {
                        int temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
        }
        //參数为byte数组的冒泡排序
        public void BubbleSort(byte[] array)
        {
            int length = array.Length;
            for (int i = 0; i <= length - 2; i++)
            {
                for (int j = length - 1; j >= 1; j--)
                { 
                //对两个元素进行交换
                    if (array[j] < array[j - 1])
                    {
                        byte temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }</span><pre name="code" class="csharp"><span style="font-size:18px;">#region Author & Version
</span>


上面是两个參数不同的冒泡函数,这两个函数除了參数不一样,其它没有差距。之后咱们就抽象一下吧!

<span style="font-size:18px;">#region Author & Version
/*
**********************************************************************************
 *作者:王鹏
 * 小组:开发小组(十期新生入学组:王美 许丹 邱慕夏 王静娜 王鹏 徐璐 卢春霞 韩欣桐)
 * 说明: B层——户口管理
 *创建日期:2015/7/29 16:31:13
 * 版本:V3.0.0
*********************************************************************************
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ITOO.Generic
{
    public class SortHelperGenericV1<T>
    {
        //參数为T的冒泡排序
        public void BubbleSort(T[] array)
        {
            int length = array.Length;
            for (int i = 0; i <= length - 2; i++)
            {
                for (int j = length - 1; j >= 1; j--)
                {
                    //对两个元素进行交换
                    if (array[j] < array[j - 1])
                    {
                        T temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
        }
    }
}
</span>
这个是报错的,在这里  if (array[j] < array[j - 1])报的错是:

主要是这里比較的时候不知道按什么因素比較。由于T的类型不确定。所以不知道按什么比較!

问题来了,方法也就来了。

给大家介绍一下这个接口

IComparable

它的介绍看百度介绍吧!IComparable
这里这句话重要

然后看实现吧:

<span style="font-size:18px;">#region Author & Version
/*
**********************************************************************************
 *作者:
 * 小组:
 * 说明: 
 *创建日期:
 * 版本:
*********************************************************************************
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ITOO.Generic
{
    public class Book : IComparable
    { 
      //价格
        private int price;

        private string title;

        public Book()
        { }
        public Book( int price, string title)
        {
            this.price = price;
            this.title = title;
        }
        //价格属性
        public int Price
        {
            get { return this.price; }
        }
        public string Title
        {
            get { return this.title; }
        }
        //实现此方法,规定比較的方法
        public int CompareTo(object obj)
        {
            Book book2 = (Book)obj;
            //return this.Price.CompareTo(book2.Price);
            return this.Title.CompareTo(book2.Title);
        }
    }
}
</span>
看看之后的冒泡排序的方法

<span style="font-size:18px;">#region Author & Version
/*
**********************************************************************************
 *作者:
 * 小组:
 * 说明: 
 *创建日期:
 * 版本:
*********************************************************************************
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ITOO.Generic
{
    public class SortHelperGenericV2<T> where T : IComparable
    {
        //參数为T的冒泡排序
        public void BubbleSort(T[] array)
        {
            int length = array.Length;
            for (int i = 0; i <= length - 2;i++ )
            {
                for (int j = length - 1; j >= 1;j-- )
                {
                    //对两个元素进行交换
                    if(array[j].CompareTo(array[j-1])<0)
                    {
                        T temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
        }
    }
}
</span>
这样我们在
CompareTo方法里面就能规定我们想要怎样进行比較了

总结:

泛型就是把一个方法或者类,不规定他的类型,等使用它的类或者方法来规定他的类型,这样就灵活多了!

想想如今,我们遇到的问题。仅仅要自己不逃避都能找到解决方法,关键在于自己的心态。




原文地址:https://www.cnblogs.com/claireyuancy/p/7265669.html