淺談c# 泛型類的應(yīng)用
泛型類封裝不是特定于具體數(shù)據(jù)類型的操作。 泛型類最常用于集合,如鏈接列表、哈希表、堆棧、隊列、樹等。 像從集合中添加和移除項這樣的操作都以大體上相同的方式執(zhí)行,與所存儲數(shù)據(jù)的類型無關(guān)。對大多集合類的操作,推薦使用 .NET Framework 類庫中所提供的類。
(1)泛型類可以繼承具體類、封閉式構(gòu)造、開放式構(gòu)造基類。
class BaseNode { }
class BaseNodeGeneric<T> { }
// 繼承具體類
class NodeConcrete<T> : BaseNode { }
//繼承封閉式構(gòu)造基類
//封閉式構(gòu)造基類指基類類型參數(shù)指定具體類型
class NodeClosed<T> : BaseNodeGeneric<int> { }
//繼承開放式構(gòu)造基類
//開放式構(gòu)造基類指基類類型參數(shù)未指定
class NodeOpen<T> : BaseNodeGeneric<T> { }
(2)基類類型參數(shù)必須在子類中指定實現(xiàn)。
//正確
class Node1 : BaseNodeGeneric<int> { }
//錯誤
//在子類中未指定父類類型參數(shù)實現(xiàn)
class Node2 : BaseNodeGeneric<T> {}
//錯誤
//在子類中未指定父類類型參數(shù)實現(xiàn)
class Node3 : T {}
class BaseNodeMultiple<T, U> { }
//正確
class Node4<T> : BaseNodeMultiple<T, int> { }
//正確
class Node5<T, U> : BaseNodeMultiple<T, U> { }
//錯誤
//在子類中未指定父類類型參數(shù)實現(xiàn)
class Node6<T> : BaseNodeMultiple<T, U> {}
(3)從開放式構(gòu)造類型繼承的泛型類必須指定約束,這些約束是基類型約束的超集或暗示基類型約束。
class NodeItem<T> where T : System.IComparable<T>, new() { }
class SpecialNodeItem<T> : NodeItem<T> where T : System.IComparable<T>, new() { }
(4)泛型類型可以使用多個類型參數(shù)和約束。
class SuperKeyType<K, V, U>
where U : System.IComparable<U>
where V : new()
{ }
(5)開放式構(gòu)造類型和封閉式構(gòu)造類型可以用作方法參數(shù)。
void Swap<T>(List<T> list1, List<T> list2)
{ }
void Swap(List<int> list1, List<int> list2)
{ }
泛型接口
(1)泛型類型參數(shù)可指定多重接口約束。
class Stack<T> where T : System.IComparable<T>, IEnumerable<T>
{
}
(2)接口可以定義多個類型參數(shù)。
interface IDictionary<K, V>
{
}
(3)類繼承規(guī)則適用接口繼承規(guī)則。(參考上面泛型類繼承)
(4)泛型接口實例
class GenericInterface
{
static void Main()
{
SortedList<Person> list = new SortedList<Person>();
string[] names = new string[]
{
"zhang san",
"li si",
"wang wu",
"zhou er",
"he yi"
};
int[] ages = new int[] { 22, 15, 30, 34, 12 };
for (int x = 0; x < 5; x++)
{
list.AddNode(new Person(names[x], ages[x]));
}
foreach (Person p in list)
{
System.Console.WriteLine(p.ToString());
}
Console.WriteLine("------------------排序-----------------------");
list.BublleSort();
foreach (Person p in list)
{
System.Console.WriteLine(p.ToString());
}
Console.Read();
}
}
public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
{
public class Node
{
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
private Node last;
public Node Last
{
get { return last; }
set { last = value; }
}
public Node(T t)
{
data = t;
next = null;
}
}
public Node firstNode;
private Node lastNode;
public void AddNode(T t)
{
Node node = new Node(t);
node.Last = lastNode;
if (lastNode != null)
lastNode.Next = node;
lastNode = node;
if (firstNode == null)
firstNode = node;
}
#region IEnumerable<T> 成員
public IEnumerator<T> GetEnumerator()
{
Node current = firstNode;
while (current != null)
{
//yield return表達(dá)式以枚舉對象返回
yield return current.Data;
current = current.Next;
}
}
#endregion
#region IEnumerable 成員
//IEnumerable < T >繼承自IEnumerable,
//因此這類必須實現(xiàn)泛型和非泛型的版本的GetEnumerator。
//在大多數(shù)情況下,非泛型方法簡單調(diào)用泛型方法。
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
{
//該方法實現(xiàn)排序
public void BublleSort()
{
if (firstNode == null || firstNode.Next == null)
return;
bool swapped;
do
{
Node last = null;
Node current = firstNode;
swapped = false;
while (current.Next != null)
{
if (current.Data.CompareTo(current.Next.Data) > 0)
{
/* 當(dāng)前節(jié)點(diǎn)大于下一個節(jié)點(diǎn),位置交換*/
Node tmp = current.Next;
current.Next = current.Next.Next;
tmp.Next = current;
if (last == null)
{
firstNode = tmp;
}
else
{
last.Next = tmp;
}
last = tmp;
swapped = true;
}
else
{
last = current;
current = current.Next;
}
}
}
while (swapped);
}
}
public class Person : System.IComparable<Person>
{
string name;
int age;
public Person(string n, int a)
{
name = n;
age = a;
}
#region IComparable<Person> 成員
public int CompareTo(Person p)
{
//按年齡排序
//return age - p.age;
//按名稱排序
int a =name.CompareTo(p.name);
return a;
}
#endregion
public override string ToString()
{
return name + ":" + age;
}
}
輸出如下:
泛型方法
包含類型參數(shù)聲明的方法即為泛型方法。
(1)泛型類的類型參數(shù)與它內(nèi)部泛型方法的類型參數(shù)一致,編譯器將生成警告 CS0693。
class GenericList<T>
{
// CS0693
void SampleMethod<T>() { }
}
(2)泛型方法的類型參數(shù)可以進(jìn)行約束。
(3)泛型方法可以使用許多類型參數(shù)進(jìn)行重載。
void DoWork() { }
void DoWork<T>() { }
void DoWork<T, U>() { }
相關(guān)文章
C#/VB.NET實現(xiàn)在PDF文檔中插入,替換或刪除圖片
這篇文章主要為大家詳細(xì)介紹了如何使用 Spire.PDF for .NET 通過程序在 PDF 文檔中插入、替換或刪除圖片,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12winform多線程組件BackgroundWorker使用
這篇文章介紹了winform多線程組件BackgroundWorker的使用方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05winform實現(xiàn)關(guān)閉按鈕失效的兩種方法
這篇文章主要介紹了winform實現(xiàn)關(guān)閉按鈕失效的兩種方法,實例分析了WinForm實現(xiàn)關(guān)閉按鈕失效的原理與所涉及的相關(guān)技巧,需要的朋友可以參考下2015-09-09C#數(shù)據(jù)類型轉(zhuǎn)換(顯式轉(zhuǎn)型、隱式轉(zhuǎn)型、強(qiáng)制轉(zhuǎn)型)
本文詳細(xì)講解了C#數(shù)據(jù)類型轉(zhuǎn)換(顯式轉(zhuǎn)型、隱式轉(zhuǎn)型、強(qiáng)制轉(zhuǎn)型),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01C#中FlagsAttribute屬性在enum中的應(yīng)用詳解
這篇文章主要介紹了C#中FlagsAttribute屬性在enum中的應(yīng)用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10解析Silverlight調(diào)用WCF/Rest異常的解決方法
本篇文章對Silverlight調(diào)用WCF/Rest異常的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05