C# 泛型編程基礎(chǔ)實(shí)例詳解
C# 泛型編程基礎(chǔ)學(xué)習(xí):最顯著的一點(diǎn)就是它參數(shù)化了類型,把類型作為參數(shù)抽象出來(lái),從而使我們?cè)趯?shí)際的運(yùn)用當(dāng)中能夠更好的實(shí)現(xiàn)代碼的重復(fù)利用,同時(shí)它提供了更強(qiáng)的類型安全,更高的效率,不過(guò)在約束方面,它只支持顯示的約束,這樣在靈活性方面就顯得不是那么好了。我覺(jué)得它之所以能夠提供更高的效率是因?yàn)榉盒驮趯?shí)例化的時(shí)候采用了 "on-demand"的模式,即按需實(shí)例化,發(fā)生在JIT(Just In Time)編譯時(shí)。
下面來(lái)看如何定義一個(gè)C# 泛型類,很簡(jiǎn)單,你只需要意識(shí)到一點(diǎn),在這里,類型已經(jīng)被參數(shù)化了:
C# 泛型編程實(shí)例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace GenericTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- //使用string,int來(lái)實(shí)例化Test類
- Test t = new Test("SHY520",22);
- //調(diào)用泛型類中的方法
- t.SetValue();
- }
- }
- /**////
- /// 定義一個(gè)泛型類,該類有兩個(gè)類型參數(shù),分別是T,S
- ///
- /// 類型參數(shù)
- /// 類型參數(shù)
- public class Test
- {
- //泛型類的類型參數(shù)可用于類成員
- private T name;
- private S age;
- public Test(T Name,S Age)
- {
- this.name = Name;
- this.age = Age;
- }
- public void SetValue()
- {
- Console.WriteLine(name.ToString());
- Console.WriteLine(age.ToString());
- }
- }
- }
上面的C# 泛型編程例子不是很恰當(dāng),目的是讓初學(xué)C# 泛型的你了解一下泛型的定義及實(shí)例化方法,如上,我們定義了一個(gè)泛型類,那么如何實(shí)現(xiàn)泛型類的繼承呢?這里需要滿足下面兩點(diǎn)中的任何一點(diǎn)即可:
1、C# 泛型類繼承中,父類的類型參數(shù)已被實(shí)例化,這種情況下子類不一定必須是C# 泛型類;
2、父類的類型參數(shù)沒(méi)有被實(shí)例化,但來(lái)源于子類,也就是說(shuō)父類和子類都是泛型類,并且二者有相同的類型參數(shù);
- //如果這樣寫的話,顯然會(huì)報(bào)找不到類型T,S的錯(cuò)誤
- public class TestChild : Test { }
- //正確的寫法應(yīng)該是
- public class TestChild : Test{ }
- public class TestChild : Test { }
- public class TestChild : Test { }
接著我們來(lái)看看泛型接口,其創(chuàng)建以及繼承規(guī)則和上面說(shuō)的泛型類是一樣的,看下面的代碼:
- public interface IList
- {
- T[] GetElements();
- }
- public interface IDictionary
- {
- void Add(K key, V value);
- }
- // 泛型接口的類型參數(shù)要么已實(shí)例化
- // 要么來(lái)源于實(shí)現(xiàn)類聲明的類型參數(shù)
- class List : IList, IDictionary
- {
- public T[] GetElements() { return null; }
- public void Add(int index, T value)
- {}
- }
在來(lái)看一下C# 泛型委托,首先我們定義一個(gè)類型參數(shù)為T的委托,然后在類中利用委托調(diào)用方法:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace GenericTest
- {
- //定義一個(gè)委托,類型參數(shù)為T,返回值類型T
- //泛型委托支持在返回值和參數(shù)上應(yīng)用類型參數(shù)
- delegate string GenericDelete(T value);
- class test
- {
- static string F(int i) { return "SHY520"; }
- static string G(string s) { return "SHY520"; }
- static void Main(string[] args)
- {
- GenericDelete G1 = G;
- GenericDelete G2 = new GenericDelete(F);
- }
- }
- }
我們?cè)賮?lái)看C# 泛型方法,C#的泛型機(jī)制只支持在方法申明上包含類型參數(shù),也即是泛型方法。特別注意的是,泛型不支持在除了方法以外的其他類/接口成員上使用類型參數(shù),但這些成員可以被包含在泛型類型中,并且可以使用泛型類型的類型參數(shù)。還有一點(diǎn)需要說(shuō)的就是,泛型方法可以在泛型類型中,也可以存在于非泛型類型中。下面我們分別看一下泛型類型的申明,調(diào)用,重載和覆蓋。
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace GenericTest
- {
- class GenericClass
- {
- //申明一個(gè)泛型方法
- public T getvalue(T t)
- {
- return t;
- }
- //調(diào)用泛型方法
- //注意:在調(diào)用泛型方法時(shí),對(duì)泛型方法的類型參數(shù)實(shí)例化
- public int useMethod()
- {
- return this.getvalue(10);
- }
- //重載getvalue方法
- public int getvalue(int i)
- {
- return i;
- }
- }
- //下面演示覆蓋
- //要注意的是,泛型方法被覆蓋時(shí),約束被默認(rèn)繼承,不需要重新指定約束關(guān)系
- abstract class Parent
- {
- public abstract K TEST(K k, V v) where K : V;
- }
- class Child : Parent
- {
- public override T TEST(T t, S s)
- {
- return t;
- }
- }
- }
C# 泛型編程基礎(chǔ)實(shí)例的基本內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C# 泛型編程基礎(chǔ)有所幫助。
【編輯推薦】