自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談C#泛型的定義、繼承、方法和約束

開發(fā) 后端
本文介紹了如何定義一個(gè)C#泛型類,以及實(shí)現(xiàn)泛型類的繼承、方法和約束。

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ù)化了:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace GenericTest  
  6. {  
  7.  class Program  
  8.  {  
  9.   static void Main(string[] args)  
  10.   {  
  11.    //使用string,int來(lái)實(shí)例化Test< T,S>類  
  12.    Test< stringint> t = new Test< stringint>("SHY520",22);  
  13.  
  14.    //調(diào)用泛型類中的方法  
  15.    t.SetValue();  
  16.   }  
  17.  }  
  18.  
  19.  /**//// < summary>  
  20.  /// 定義一個(gè)泛型類,該類有兩個(gè)類型參數(shù),分別是T,S  
  21.  /// http://pw.cnblogs.com  
  22.  /// < /summary>  
  23.  /// < typeparam name="T">類型參數(shù)< /typeparam>  
  24.  /// < typeparam name="S">類型參數(shù)< /typeparam>  
  25.  public class Test< T,S>  
  26.  {  
  27.   //泛型類的類型參數(shù)可用于類成員  
  28.   private T name;  
  29.   private S age;  
  30.  
  31.   public Test(T Name,S Age)  
  32.   {  
  33.    this.name = Name;  
  34.    this.age = Age;  
  35.   }  
  36.  
  37.   public void SetValue()  
  38.   {  
  39.    Console.WriteLine(name.ToString());  
  40.    Console.WriteLine(age.ToString());  
  41.   }  
  42.  }  

上面的例子不是很恰當(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ù);

  1. //如果這樣寫的話,顯然會(huì)報(bào)找不到類型T,S的錯(cuò)誤  
  2. public class TestChild : Test< T, S> { }  
  3.  
  4. //正確的寫法應(yīng)該是  
  5. public class TestChild : Test< stringint>{ }  
  6. public class TestChild< T, S> : Test< T, S> { }  
  7. public class TestChild< T, S> : Test< String, int> { } 

接著我們來(lái)看看泛型接口,其創(chuàng)建以及繼承規(guī)則和上面說(shuō)的泛型類是一樣的,看下面的代碼:

  1. public interface IList< T>   
  2. {  
  3.  T[] GetElements();  
  4. }   
  5. public interface IDictionary< K,V>   
  6. {  
  7.  void Add(K key, V value);   
  8. }  
  9.  
  10. // 泛型接口的類型參數(shù)要么已實(shí)例化  
  11. // 要么來(lái)源于實(shí)現(xiàn)類聲明的類型參數(shù)  
  12. class List< T> : IList< T>, IDictionary< int, T>   
  13. {  
  14.  public T[] GetElements() { return null; }  
  15.  public void Add(int index, T value)   
  16.  {}  

在來(lái)看一下C#泛型委托,首先我們定義一個(gè)類型參數(shù)為T的委托,然后在類中利用委托調(diào)用方法:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace GenericTest  
  6. {  
  7.  //定義一個(gè)委托,類型參數(shù)為T,返回值類型T  
  8.  //泛型委托支持在返回值和參數(shù)上應(yīng)用類型參數(shù)  
  9.  delegate string GenericDelete< T>(T value);  
  10.  
  11.  class test  
  12.  {  
  13.   static string F(int i) { return "SHY520"; }  
  14.   static string G(string s) { return "SHY520"; }  
  15.  
  16.   static void Main(string[] args)  
  17.   {  
  18.    GenericDelete< string> G1 = G;  
  19.    GenericDelete< int> G2 = new GenericDelete< int>(F);  
  20.   }  
  21.  }   

我們?cè)賮?lái)看C#泛型方法,C#的泛型機(jī)制只支持在方法申明上包含類型參數(shù),也即是泛型方法。特別注意的是,泛型不支持在除了方法以外的其他類/接口成員上使用類型參數(shù),但這些成員可以被包含在泛型類型中,并且可以使用泛型類型的類型參數(shù)。還有一點(diǎn)需要說(shuō)的就是,泛型方法可以在泛型類型中,也可以存在于非泛型類型中。下面我們分別看一下泛型類型的申明,調(diào)用,重載和覆蓋。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace GenericTest  
  6. {  
  7.  class GenericClass  
  8.  {  
  9.   //申明一個(gè)泛型方法  
  10.   public T getvalue< T>(T t)  
  11.   {  
  12.    return t;  
  13.   }  
  14.  
  15.   //調(diào)用泛型方法  
  16.   //注意:在調(diào)用泛型方法時(shí),對(duì)泛型方法的類型參數(shù)實(shí)例化  
  17.   public int useMethod()  
  18.   {  
  19.    return this.getvalue< int>(10);  
  20.   }  
  21.  
  22.   //重載getvalue方法  
  23.   public int getvalue(int i)  
  24.   {  
  25.    return i;  
  26.   }  
  27.  }  
  28.  
  29.  //下面演示覆蓋  
  30.  //要注意的是,泛型方法被覆蓋時(shí),約束被默認(rèn)繼承,不需要重新指定約束關(guān)系  
  31.  abstract class Parent  
  32.  {  
  33.   public abstract K TEST< K, V>(K k, V v) where K : V;  
  34.  }  
  35.  
  36.  class Child : Parent  
  37.  {  
  38.   public override T TEST< T, S>(T t, S s)  
  39.   {  
  40.    return t;  
  41.   }  
  42.  }  

***我們來(lái)看一下C#泛型中的約束:

C#中的泛型只支持顯示的約束,因?yàn)檫@樣才能保證C#所要求的類型安全,但顯示的約束并非時(shí)必須的,如果不加約束,泛型類型參數(shù)將只能訪問(wèn)System.Object類型中的公有方法?!帮@式約束”由where子句表達(dá),可以指定“基類約束”,“接口約束”,“構(gòu)造器約束”,“值類型/引用類型約束”共四種約束。下面的例子來(lái)源于李建忠老師的講座PPT。

1、基類約束:

  1. class A { public void F1() {} }   
  2. class B { public void F2() {} }   
  3. class C< S,T>   
  4. where S: A // S繼承自A   
  5. where T: B // T繼承自B   
  6. {   
  7.  // 可以在類型為S的變量上調(diào)用F1,  
  8.  // 可以在類型為T的變量上調(diào)用F2   
  9. }  

2、接口約束

  1. interface IPrintable { void Print(); }  
  2. interface IComparable< T> { int CompareTo(T v);}  
  3. interface IKeyProvider< T> { T GetKey(); }  
  4. class Dictionary< K,V>   
  5. where K: IComparable< K>   
  6. where V: IPrintable, IKeyProvider< K>   
  7. {   
  8.  // 可以在類型為K的變量上調(diào)用CompareTo,   
  9.  // 可以在類型為V的變量上調(diào)用Print和GetKey   

3、構(gòu)造器約束

  1. class A { public A() { } }   
  2. class B { public B(int i) { } }   
  3. class C< T>   
  4. where T : new()   
  5. {   
  6.  //可以在其中使用T t=new T();   
  7. }   
  8. C< A> c=new C< A>(); //可以,A有無(wú)參構(gòu)造器  
  9. C< B> c=new C< B>(); //錯(cuò)誤,B沒(méi)有無(wú)參構(gòu)造器 

4、值/引用類型約束

  1. public struct A { }   
  2. public class B { }   
  3. class C< T>   
  4. where T : struct   
  5. {   
  6.  // T在這里面是一個(gè)值類型   
  7. }   
  8. C< A> c=new C< A>(); //可以,A是一個(gè)值類型  
  9. C< B> c=new C< B>(); //錯(cuò)誤,B是一個(gè)引用類型 

【編輯推薦】

  1. C# winForm自定義鼠標(biāo)樣式的兩種方法
  2. C#自定義消息框的設(shè)置圖解
  3. 掌握C#自定義泛型類:從初始化說(shuō)起
  4. C#存儲(chǔ)過(guò)程的循序漸進(jìn)
  5. 存儲(chǔ)過(guò)程的優(yōu)勢(shì)及其調(diào)用方法介紹
責(zé)任編輯:book05 來(lái)源: hi.baidu
相關(guān)推薦

2009-06-24 10:25:25

C#泛型

2009-08-24 14:43:35

C# 泛型

2009-08-24 13:31:38

C# 泛型約束

2009-08-24 13:41:23

C# 泛型約束

2009-08-24 13:52:04

C# 泛型約束

2009-08-24 12:58:15

C# 泛型約束

2013-03-20 09:27:33

C#泛型

2009-08-24 16:19:42

C# 泛型方法

2009-08-24 15:28:19

C# 泛型方法

2009-08-26 09:36:03

C#泛型

2009-06-16 10:20:05

多繼承C#

2009-09-01 16:14:11

C#泛型

2009-08-24 10:29:39

C# 泛型

2009-08-24 15:12:13

C# 泛型接口

2009-09-02 17:38:16

C#泛型支持

2009-08-24 18:15:24

C# Dictiona

2009-08-24 15:38:21

C# 泛型數(shù)組

2009-08-24 14:51:25

C# 泛型泛型類型

2009-09-04 17:34:11

C#CC++

2009-08-25 16:16:43

C# oledbcon
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)