從簡單的例子理解泛型
話說有家影視公司選拔偶像派男主角,導演說了,男演員,身高是王道。于是有下面代碼:
public Boy(string name, int height) {
this.mName = name;
this.mHeight = height;
}
}
//演員選拔類
public class Compare
{
//導演導超女出生,喜歡一對一PK
public Boy WhoIsBetter(Boy boy1, Boy boy2)
{
if (boy1.Height > boy2.Height)
{
return boy1;
}
else
{
return boy2;
}
}
}
//測試
static void Main(string[] args)
{
Boy boy1 = new Boy("潘長江", 165);
Boy boy2 = new Boy("劉德華", 175);
Console.WriteLine(new Compare().WhoIsBetter(boy1, boy2).Name);
Console.ReadLine();
}
public Girl(string name, int weight){
this.mName = name;
this.mWeight = weight;
}
}
//演員選拔類中添加一個女演員方法
public class Compare
{
//男演員身高是王道
public Boy WhoIsBetter(Boy boy1, Boy boy2)
{
if (boy1.Height > boy2.Height)
{
return boy1;
}
else
{
return boy2;
}
}
//女演員苗條是王道
public Girl WhoIsBetter(Girl girl1, Girl girl2)
{
if (girl1.Weight < girl2.Weight)
{
return girl1;
}
else
{
return girl2;
}
}
}
//測試
static void Main(string[] args)
{
Boy boy1 = new Boy("潘長江", 165);
Boy boy2 = new Boy("劉德華", 175);
Girl girl1 = new Girl("鞏俐", 120);
Girl girl2 = new Girl("周迅", 80);
Console.WriteLine(new Compare().WhoIsBetter(boy1, boy2).Name);
Console.WriteLine(new Compare().WhoIsBetter(girl1, girl2).Name);
Console.ReadLine();
}
public Boy(string name, int height) {
this.mName = name;
this.mHeight = height;
}
public int CompareTo(object obj)
{
//比較身高
return this.mHeight - ((Boy)obj).Height;
}
}
/// <summary>
/// 女演員:實現(xiàn)IComparable接口
/// </summary>
public class Girl : IComparable
{
//姓名
private string mName;
//體重 www.survivalescaperooms.com
private int mWeight;
public string Name
{
get { return this.mName; }
}
public int Weight
{
get { return this.mWeight; }
}
public Girl(string name, int weight){
this.mName = name;
this.mWeight = weight;
}
public int CompareTo(object obj)
{
//比較體重
return ((Girl)obj).Weight - this.mWeight;
}
}
Girl girl1 = new Girl("鞏俐", 120);
Girl girl2 = new Girl("周迅", 80);
Console.WriteLine(((Boy)new Compare().WhoIsBetter(boy1, boy2)).Name);
Console.WriteLine(((Girl)new Compare().WhoIsBetter(girl1, girl2)).Name);
Console.WriteLine(new Compare().WhoIsBetter(boy1.Height, boy2.Height));
Console.WriteLine(new Compare().WhoIsBetter(girl1.Weight, girl2.Weight));
Console.ReadLine();
}
Girl girl1 = new Girl("鞏俐", 120);
Girl girl2 = new Girl("周迅", 80);
Console.WriteLine(((Boy)new Compare().WhoIsBetter(boy1, girl1)).Name);
Console.ReadLine();
}
Girl girl1 = new Girl("鞏俐", 120);
Girl girl2 = new Girl("周迅", 80);
Console.WriteLine((new Compare<Boy>().WhoIsBetter(boy1, boy2)).Name);
Console.WriteLine((new Compare<Girl>().WhoIsBetter(girl1, girl2)).Name);
Console.WriteLine(new Compare<int>().WhoIsBetter(boy1.Height, boy2.Height));
Console.WriteLine(new Compare<string>().WhoIsBetter(boy1.Name, girl1.Name));
Console.ReadLine();
}
約束 說明
T:結構 類型參數(shù)必須是值類型。可以指定除Nullable 以外的任何值類型。
T:類 類型參數(shù)必須是引用類型;這一點也適用于任何類、接口、委托或數(shù)組類型。
T:new() 類型參數(shù)必須具有無參數(shù)的公共構造函數(shù)。當與其他約束一起使用時,new() 約束必須最后指定。
T:<基類名> 類型參數(shù)必須是指定的基類或派生自指定的基類。
T:<接口名稱> 類型參數(shù)必須是指定的接口或實現(xiàn)指定的接口。可以指定多個接口約束。約束接口也可以是泛型的。
T:U 為T 提供的類型參數(shù)必須是為U 提供的參數(shù)或派生自為U 提供的參數(shù)。
新聞熱點
疑難解答