淺談C#泛型的定義、繼承、方法和約束
C#泛型參數(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ù)化了:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace GenericTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- //使用string,int來(lái)實(shí)例化Test< T,S>類
- Test< string, int> t = new Test< string, int>("SHY520",22);
- //調(diào)用泛型類中的方法
- t.SetValue();
- }
- }
- /**//// < summary>
- /// 定義一個(gè)泛型類,該類有兩個(gè)類型參數(shù),分別是T,S
- /// http://pw.cnblogs.com
- /// < /summary>
- /// < typeparam name="T">類型參數(shù)< /typeparam>
- /// < typeparam name="S">類型參數(shù)< /typeparam>
- public class Test< T,S>
- {
- //泛型類的類型參數(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());
- }
- }
- }
上面的例子不是很恰當(dāng),目的是讓初學(xué)泛型的你了解一下泛型的定義及實(shí)例化方法,如上,我們定義了一個(gè)泛型類,那么如何實(shí)現(xiàn)C#泛型類的繼承呢?這里需要滿足下面兩點(diǎn)中的任何一點(diǎn)即可:
1、泛型類繼承中,父類的類型參數(shù)已被實(shí)例化,這種情況下子類不一定必須是泛型類;
2、父類的類型參數(shù)沒(méi)有被實(shí)例化,但來(lái)源于子類,也就是說(shuō)父類和子類都是泛型類,并且二者有相同的類型參數(shù);
- //如果這樣寫的話,顯然會(huì)報(bào)找不到類型T,S的錯(cuò)誤
- public class TestChild : Test< T, S> { }
- //正確的寫法應(yīng)該是
- public class TestChild : Test< string, int>{ }
- public class TestChild< T, S> : Test< T, S> { }
- public class TestChild< T, S> : Test< String, int> { }
接著我們來(lái)看看泛型接口,其創(chuàng)建以及繼承規(guī)則和上面說(shuō)的泛型類是一樣的,看下面的代碼:
- public interface IList< T>
- {
- T[] GetElements();
- }
- public interface IDictionary< K,V>
- {
- void Add(K key, V value);
- }
- // 泛型接口的類型參數(shù)要么已實(shí)例化
- // 要么來(lái)源于實(shí)現(xiàn)類聲明的類型參數(shù)
- class List< T> : IList< T>, IDictionary< int, T>
- {
- 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>(T value);
- class test
- {
- static string F(int i) { return "SHY520"; }
- static string G(string s) { return "SHY520"; }
- static void Main(string[] args)
- {
- GenericDelete< string> G1 = G;
- GenericDelete< int> G2 = new GenericDelete< int>(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 t)
- {
- return t;
- }
- //調(diào)用泛型方法
- //注意:在調(diào)用泛型方法時(shí),對(duì)泛型方法的類型參數(shù)實(shí)例化
- public int useMethod()
- {
- return this.getvalue< int>(10);
- }
- //重載getvalue方法
- public int getvalue(int i)
- {
- return i;
- }
- }
- //下面演示覆蓋
- //要注意的是,泛型方法被覆蓋時(shí),約束被默認(rèn)繼承,不需要重新指定約束關(guān)系
- abstract class Parent
- {
- public abstract K TEST< K, V>(K k, V v) where K : V;
- }
- class Child : Parent
- {
- public override T TEST< T, S>(T t, S s)
- {
- return t;
- }
- }
- }
***我們來(lái)看一下C#泛型中的約束:
C#中的泛型只支持顯示的約束,因?yàn)檫@樣才能保證C#所要求的類型安全,但顯示的約束并非時(shí)必須的,如果不加約束,泛型類型參數(shù)將只能訪問(wèn)System.Object類型中的公有方法?!帮@式約束”由where子句表達(dá),可以指定“基類約束”,“接口約束”,“構(gòu)造器約束”,“值類型/引用類型約束”共四種約束。下面的例子來(lái)源于李建忠老師的講座PPT。
1、基類約束:
- class A { public void F1() {} }
- class B { public void F2() {} }
- class C< S,T>
- where S: A // S繼承自A
- where T: B // T繼承自B
- {
- // 可以在類型為S的變量上調(diào)用F1,
- // 可以在類型為T的變量上調(diào)用F2
- }
2、接口約束
- interface IPrintable { void Print(); }
- interface IComparable< T> { int CompareTo(T v);}
- interface IKeyProvider< T> { T GetKey(); }
- class Dictionary< K,V>
- where K: IComparable< K>
- where V: IPrintable, IKeyProvider< K>
- {
- // 可以在類型為K的變量上調(diào)用CompareTo,
- // 可以在類型為V的變量上調(diào)用Print和GetKey
- }
3、構(gòu)造器約束
- class A { public A() { } }
- class B { public B(int i) { } }
- class C< T>
- where T : new()
- {
- //可以在其中使用T t=new T();
- }
- C< A> c=new C< A>(); //可以,A有無(wú)參構(gòu)造器
- C< B> c=new C< B>(); //錯(cuò)誤,B沒(méi)有無(wú)參構(gòu)造器
4、值/引用類型約束
- public struct A { }
- public class B { }
- class C< T>
- where T : struct
- {
- // T在這里面是一個(gè)值類型
- }
- C< A> c=new C< A>(); //可以,A是一個(gè)值類型
- C< B> c=new C< B>(); //錯(cuò)誤,B是一個(gè)引用類型
【編輯推薦】