c#自定義泛型類的實(shí)現(xiàn)
更新時(shí)間:2013年05月31日 11:50:42 作者:
本篇文章是對(duì)c#中自定義泛型類的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
閑來無(wú)事,自己研究了泛型類的簡(jiǎn)單的使用,
where表示泛型約束,表示泛型類型中的參數(shù)只能是car類型,IEnumerable是一個(gè)接口,一個(gè)集合要支持FOREAch遍歷,
必須實(shí)現(xiàn)IEnumerable接口
public class Car
{
public string PetName;
public int Speed;
public Car(string name, int currentSpeed)
{
PetName = name;
Speed = currentSpeed;
}
public Car() { }
}
public class CarCollection<T> : IEnumerable<T> where T : Car
{
private List<T> Tcars = new List<T>();
//添加
public void AddCar(T t)
{
Tcars.Add(t);
}
//獲取其中一個(gè)
public T GetCar(int pos)
{
return Tcars[pos];
}
public int Count()
{
return Tcars.Count;
}
#region IEnumerable<T> 成員
public IEnumerator<T> GetEnumerator()
{
return Tcars.GetEnumerator();
}
#endregion
#region IEnumerable 成員
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return Tcars.GetEnumerator();
}
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
Car car1 = new Car("one", 150);
Car car2= new Car("two", 50);
Car car3 = new Car("three", 150);
CarCollection<Car> cars = new CarCollection<Car>();
cars.AddCar(car1);
cars.AddCar(car2);
cars.AddCar(car3);
MessageBox.Show(cars.Count().ToString());
foreach (Car item in cars)
{
MessageBox.Show(item.PetName+"--"+item.Speed.ToString());
}
}
where表示泛型約束,表示泛型類型中的參數(shù)只能是car類型,IEnumerable是一個(gè)接口,一個(gè)集合要支持FOREAch遍歷,
必須實(shí)現(xiàn)IEnumerable接口
復(fù)制代碼 代碼如下:
public class Car
{
public string PetName;
public int Speed;
public Car(string name, int currentSpeed)
{
PetName = name;
Speed = currentSpeed;
}
public Car() { }
}
public class CarCollection<T> : IEnumerable<T> where T : Car
{
private List<T> Tcars = new List<T>();
//添加
public void AddCar(T t)
{
Tcars.Add(t);
}
//獲取其中一個(gè)
public T GetCar(int pos)
{
return Tcars[pos];
}
public int Count()
{
return Tcars.Count;
}
#region IEnumerable<T> 成員
public IEnumerator<T> GetEnumerator()
{
return Tcars.GetEnumerator();
}
#endregion
#region IEnumerable 成員
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return Tcars.GetEnumerator();
}
#endregion
}
復(fù)制代碼 代碼如下:
private void button1_Click(object sender, EventArgs e)
{
Car car1 = new Car("one", 150);
Car car2= new Car("two", 50);
Car car3 = new Car("three", 150);
CarCollection<Car> cars = new CarCollection<Car>();
cars.AddCar(car1);
cars.AddCar(car2);
cars.AddCar(car3);
MessageBox.Show(cars.Count().ToString());
foreach (Car item in cars)
{
MessageBox.Show(item.PetName+"--"+item.Speed.ToString());
}
}
相關(guān)文章
C#簡(jiǎn)單實(shí)現(xiàn)在網(wǎng)頁(yè)上發(fā)郵件的案例
本文分享一個(gè)C#利用SMTP發(fā)送郵件的案例,提供了前后臺(tái)代碼,方便大家學(xué)習(xí)。2016-03-03
C#使用Datatable導(dǎo)入sqlserver數(shù)據(jù)庫(kù)的三種方法
本文主要介紹了C#使用Datatable導(dǎo)入sqlserver數(shù)據(jù)庫(kù)的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
C#實(shí)現(xiàn)動(dòng)態(tài)加載dll的方法
這篇文章主要介紹了C#實(shí)現(xiàn)動(dòng)態(tài)加載dll的方法,涉及針對(duì)動(dòng)態(tài)鏈接庫(kù)的靈活操作技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
C#中的數(shù)組作為參數(shù)傳遞所引發(fā)的問題
這篇文章主要介紹了C#中的數(shù)組作為參數(shù)傳遞所引發(fā)的問題 的相關(guān)資料,需要的朋友可以參考下2016-03-03
Unity3D實(shí)現(xiàn)分頁(yè)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)分頁(yè)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#使用Stack類進(jìn)行堆棧設(shè)計(jì)詳解
C#中的堆棧由System.Collections.Generic命名空間中的Stack類定義,那么下面就跟隨小編一起學(xué)習(xí)一下C#如何Stack類進(jìn)行堆棧設(shè)計(jì)吧2024-03-03
C#簡(jiǎn)單實(shí)現(xiàn)IOC容器的示例代碼
IoC 的原理是通過將對(duì)象的創(chuàng)建和依賴關(guān)系的管理交給外部容器來實(shí)現(xiàn),從而降低了代碼的耦合度,提高了代碼的可維護(hù)性和可測(cè)試性,下面我們就來看看如何通過C#實(shí)現(xiàn)一個(gè)IOC容器吧2024-02-02

