淺談c# 泛型類的應用

來源:互聯網
上載者:User

泛型類
泛型類封裝不是特定於具體資料類型的操作。 泛型類最常用於集合,如連結清單、雜湊表、堆棧、隊列、樹等。 像從集合中添加和移除項這樣的操作都以大體上相同的方式執行,與所儲存資料的類型無關。對大多集合類的操作,推薦使用 .NET Framework 類庫中所提供的類。
(1)泛型類可以繼承具體類、封閉式構造、開放式構造基類。
複製代碼 代碼如下:class BaseNode { }
class BaseNodeGeneric<T> { }
// 繼承具體類
class NodeConcrete<T> : BaseNode { }
//繼承封閉式構造基類
//封閉式構造基類指基類型別參數指定具體類型
class NodeClosed<T> : BaseNodeGeneric<int> { }
//繼承開放式構造基類
//開放式構造基類指基類型別參數未指定
class NodeOpen<T> : BaseNodeGeneric<T> { }

(2)基類型別參數必須在子類中指定實現。
複製代碼 代碼如下://正確
class Node1 : BaseNodeGeneric<int> { }
//錯誤
//在子類中未指定父類型別參數實現
class Node2 : BaseNodeGeneric<T> {}
//錯誤
//在子類中未指定父類型別參數實現
class Node3 : T {}
class BaseNodeMultiple<T, U> { }
//正確
class Node4<T> : BaseNodeMultiple<T, int> { }
//正確
class Node5<T, U> : BaseNodeMultiple<T, U> { }
//錯誤
//在子類中未指定父類型別參數實現
class Node6<T> : BaseNodeMultiple<T, U> {}

(3)從開放式構造類型繼承的泛型類必須指定約束,這些約束是基底類型約束的超集或暗示基底類型約束。
複製代碼 代碼如下:class NodeItem<T> where T : System.IComparable<T>, new() { }
class SpecialNodeItem<T> : NodeItem<T> where T : System.IComparable<T>, new() { }

(4)泛型型別可以使用多個型別參數和約束。
複製代碼 代碼如下:class SuperKeyType<K, V, U>
where U : System.IComparable<U>
where V : new()
{ }

(5)開放式構造類型和封閉式構造類型可以用作方法參數。複製代碼 代碼如下:void Swap<T>(List<T> list1, List<T> list2)
{ }
void Swap(List<int> list1, List<int> list2)
{ }

泛型介面
(1)泛型型別參數可指定多重介面約束。
複製代碼 代碼如下:class Stack<T> where T : System.IComparable<T>, IEnumerable<T>
{
}

(2)介面可以定義多個型別參數。
複製代碼 代碼如下:interface IDictionary<K, V>
{
}

(3)類繼承規則適用介面繼承規則。(參考上面泛型類繼承)
(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運算式以枚舉對象返回
yield return current.Data;
current = current.Next;
}
}
#endregion
#region IEnumerable 成員
//IEnumerable < T >繼承自IEnumerable,
//因此這類必須實現泛型和非泛型的版本的GetEnumerator。
//在大多數情況下,非泛型方法簡單調用泛型方法。
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
{
//該方法實現排序
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)
{
/* 當前節點大於下一個節點,位置交換*/
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;
}
}

輸出如下:

泛型方法
包含型別參數聲明的方法即為泛型方法。
(1)泛型類的型別參數與它內部泛型方法的型別參數一致,編譯器將產生警告 CS0693。

複製代碼 代碼如下:class GenericList<T>
{
// CS0693
void SampleMethod<T>() { }
}

(2)泛型方法的型別參數可以進行約束。
(3)泛型方法可以使用許多型別參數進行重載。
複製代碼 代碼如下:void DoWork() { }
void DoWork<T>() { }
void DoWork<T, U>() { }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.