白話C#:泛型
摘要
長期使用C#的朋友大多都常常用到泛型,本文通過創建自己的泛型類來介紹一下泛型,用到泛型的時候你就會發現,泛型其實就像一個口袋,你可以很方便地往裏面裝東西,只是在第一次使用這個口袋的時候要注意聲明它只能裝什麼樣類型的東西,以後就不能裝錯了。
泛型是C# 2.0版本纔開始有的語言特性,不過“泛型”這個概念並不是最先出現在編程領域的,例如C++中的模板。
List就是一個泛型應用。你可以在需要時聲明一個強類型的List實例,然後隨意地往裏面添加、刪除和查詢同一類型的元素。泛型就是一個非常方便的數據結構,長期使用C#的朋友大多都常常用到泛型。本文就簡單地通過創建自己的泛型類來介紹一下泛型,希望能夠加深初學者對泛型(這個名字很奇怪的東西)的認識和理解。
用到泛型的時候你就會發現,泛型其實就像一個口袋,你可以很方便地往裏面裝東西,只是在第一次使用這個口袋的時候要注意聲明它只能裝什麼樣類型的東西,以後就不能裝錯了。那麼我們就用錢包爲例吧,我們首先描述一下錢包。錢包的用途不外乎是裝點兒東西,當然,除了錢還可以裝其它很多東西,例如銀行卡、便籤條、照片等等,但是這些東西有些共同的地方,至少是尺寸方面不能超過錢包的限制,誰可以把冰箱也揣在錢包裏呢?因此,我們在設計能裝進錢包的物品的類的時候就要考慮到尺寸的因素。
1: public class WalletThingBase
2: {
3: protected readonly int MaxLength = 10;
4: protected readonly int MaxWidth = 7;
5: protected readonly int MaxThickness = 1;
6:
7: private int _length = 0;
8: public int Length
9: {
10: get { return this._length; }
11: set
12: {
13: if (value <= 0 || value > this.MaxLength)
14: {
15: throw new ArgumentException("Length is invalid.");
16: }
17:
18: this._length = value;
19: }
20: }
21:
22: private int _width = 0;
23: public int Width
24: {
25: get { return this._width; }
26: set
27: {
28: if (value <= 0 || value > this.MaxWidth)
29: {
30: throw new ArgumentException("Width is invalid.");
31: }
32:
33: this._width = value;
34: }
35: }
36:
37: private int _thickness = 0;
38: public int Thickness
39: {
40: get { return this._thickness; }
41: set
42: {
43: if (value <= 0 || value > this.MaxThickness)
44: {
45: throw new ArgumentException("Thickness is invalid.");
46: }
47:
48: this._thickness = value;
49: }
50: }
51:
52: public WalletThingBase(int length, int width, int thickness)
53: {
54: this.Length = length;
55: this.Width = width;
56: this.Thickness = thickness;
57: }
58: }
接下來我們來派生幾個類吧,銀行卡以及信用卡:
1: public class BankCard : WalletThingBase
2: {
3: public int ID { get; set; }
4: public string Name { get; set; }
5: public string Password { get; set; }
6:
7: public BankCard(int length, int width, int thickness)
8: : base(length, width, thickness)
9: {
10: }
11: }
12:
13: public class CreditCard : BankCard
14: {
15: public decimal Overdraft { get; set; }
16:
17: public CreditCard(int length, int width, int thickness)
18: : base(length, width, thickness)
19: {
20: }
21: }
通過上面的代碼可以看出,在創建派生自WalletThingBase類的所有類的時候,都會先檢驗其尺寸是否超標,如果是尺寸過大就不允許創建,也就表示你不能把它放進你的錢包。顯然,銀行卡儘管規格各異,但都是可以的。
接下來,我們就要來設計錢包這個類了。我們可以藉助List來簡化我們的設計工作,最需要注意的其實就是嚴格把關,凡是非WallThingBase派生類都不允許進入,另外,還得簡單提供一些放東西和取東西的函數,這樣,一個基本的錢包就設計出來了。
1: public class Wallet : CollectionBase
2: {
3: public Wallet()
4: {
5: Type baseType = typeof(T).BaseType;
6:
7: while (baseType != null
8: && baseType != typeof(Object)
9: && baseType.BaseType != typeof(Object))
10: {
11: baseType = baseType.BaseType;
12: }
13:
14: if (baseType != typeof(WalletThingBase))
15: {
16: throw new Exception(typeof(T).ToString() + " cannot be put into wallet.");
17: }
18: }
19:
20: public T this[int index]
21: {
22: get { return (T)List[index]; }
23: set { List[index] = value; }
24: }
25:
26: public int Add(T item)
27: {
28: return List.Add(item);
29: }
30:
31: public void Remove(T item)
32: {
33: List.Remove(item);
34: }
35: }
泛型是一種很具有親和力的語言特性,很容易讓人接受也很容易讓人喜歡上它,藉助泛型的便利,盡情享受C#開發樂趣吧!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.