记得在做机房的时候遇到这样的问题,两个函数只是参数类型不一样,其他基本都一样,可是不知道怎样做才能减少这种情况,那个时候感觉这个问题挺别扭的,后来听大家都在利用泛型集合,我也就用了,至于为什么用,有什么好处,当时不太理解,今天听了讲解,认认真真的把代码实现了,焕然大悟,如果当初我多思考思考就不至于现在才弄懂这个问题,学习就是这样,有时候感觉自己走的挺好的,不愿意研究那似懂的问题,其实到最后是逃不过的!现在把之前的课补上吧!
<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
这里这句话重要
然后看实现吧:
<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方法里面就能规定我们想要如何进行比较了
总结:
泛型就是把一个方法或者类,不规定他的类型,等使用它的类或者方法来规定他的类型,这样就灵活多了!
想想现在,我们遇到的问题,只要自己不逃避都能找到解决方法,关键在于自己的心态!