C#中泛型特性2-约束

来源:互联网 发布:阿里云实名认证 编辑:程序博客网 时间:2024/06/05 06:20
  1. 让我们先看看不用约束的代码
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace 泛型
  7. {
  8.     class 泛型的特性
  9.     {
  10.         public static void Main()
  11.         {
  12.             DocumentManager<IDocument> manager = new DocumentManager<IDocument>();
  13.             manager.AddDocument( new Document( "aladdin" , "aladdin is a super man") );
  14.             manager.AddDocument(new Document("zhao""jacky is a super man"));
  15.             manager.AddDocument(new Document("jacky""jacky is a super man"));
  16.             manager.DisplayAllDocuments();
  17.             
  18.             Console.ReadLine();
  19.         }
  20.     }
  21.     interface IDocument
  22.     {
  23.         string Title { getset; }
  24.         string Content { get;set;} 
  25.     }
  26.     public class Document : IDocument
  27.     {
  28.         private string title;
  29.         private string content;
  30.         public Document(string title, string content)
  31.         {
  32.             this.title = title;
  33.             this.content = content;
  34.         }
  35.         public string Title
  36.         {
  37.             get
  38.             {
  39.                 return this.title;
  40.             }
  41.             set
  42.             {
  43.                 this.title = value;
  44.             }
  45.         }
  46.         public string Content
  47.         {
  48.             get
  49.             {
  50.                 return this.content;
  51.             }
  52.             set
  53.             {
  54.                 this.content = value;
  55.             }
  56.         }
  57.     }
  58.     class DocumentManager<T>
  59.     {
  60.         //表示对象先进先出的集合
  61.         private readonly Queue<T> documentQueue = new Queue<T>();
  62.         public void AddDocument(T doc)
  63.         {
  64.             lock (this)
  65.             {
  66.                 documentQueue.Enqueue(doc);
  67.             }
  68.         }
  69.         public bool IsDocumentAvailable
  70.         {
  71.             get { return documentQueue.Count > 0; }
  72.         }
  73.         public T GetDocument()
  74.         {
  75.             T doc = default(T);
  76.             lock (this)
  77.             {
  78.                 doc = documentQueue.Dequeue();
  79.             }
  80.             return doc;
  81.         }
  82.         //如果要显示全部的文档,跌代输出,在foreach中则要进行转型
  83.         public void DisplayAllDocuments()
  84.         {
  85.             //些处没有使用约束,如果T类型没有在客户代码中定义为Idocument接口,则会出现异常
  86.             foreach (T doc in this.documentQueue)
  87.             {
  88.                 Console.WriteLine( ((IDocument)doc).Title );
  89.                 Console.WriteLine(((IDocument)doc).Content);
  90.                 Console.WriteLine( "---------------------------");
  91.             }
  92.         }
  93.     }
  94. }
  95. ---------------------------------------------------------------------
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. namespace 泛型
    6. {
    7.     class 泛型的特性
    8.     {
    9.         public static void Main()
    10.         {
    11.             //例用泛型约束后,只能使用指定的类型为类实例化
    12.             DocumentManager<IDocument> manager = new DocumentManager<IDocument>();
    13.             manager.AddDocument(new Document("aladdin""aladdin is a super man"));
    14.             manager.AddDocument(new Document("zhao""jacky is a super man"));
    15.             manager.AddDocument(new Document("jacky""jacky is a super man"));
    16.             manager.DisplayAllDocuments();
    17.             while (manager.IsDocumentAvailable)
    18.             {
    19.                 IDocument doc = manager.GetDocument() ;
    20.                 Console.WriteLine( doc.Title );
    21.                 Console.WriteLine(doc.Content);
    22.             }
    23.             Console.ReadLine();
    24.         }
    25.     }
    26.     interface IDocument
    27.     {
    28.         string Title { getset; }
    29.         string Content { getset; }
    30.     }
    31.     public class Document : IDocument
    32.     {
    33.         private string title;
    34.         private string content;
    35.         public Document(string title, string content)
    36.         {
    37.             this.title = title;
    38.             this.content = content;
    39.         }
    40.         public string Title
    41.         {
    42.             get
    43.             {
    44.                 return this.title;
    45.             }
    46.             set
    47.             {
    48.                 this.title = value;
    49.             }
    50.         }
    51.         public string Content
    52.         {
    53.             get
    54.             {
    55.                 return this.content;
    56.             }
    57.             set
    58.             {
    59.                 this.content = value;
    60.             }
    61.         }
    62.     }
    63.     //指定TDocument类型执行的接口是IDocument
    64.     //泛型还有好几个约束方式
    65.     //1 where T : struct 使用结构约束,类型必须是值类型
    66.     //2 where T : class 使用类约束,T类型必须是引用类型
    67.     //3 where T :IFoo(接口) 类型必须引用接口(我们就是用的这种)
    68.     //4 where T : Foo(类) 类型必须是集成这个类
    69.     //5 where T : new() 这是一个构造函数约束,T类型必须拥有一个默认构造函数
    70.     //6 where T : U 指定类型派生于另一个泛型类型,这叫做 裸类型约束
    71.     class DocumentManager<TDocument> where TDocument : IDocument
    72.     {
    73.         //表示对象先进先出的集合
    74.         private readonly Queue<TDocument> documentQueue = new Queue<TDocument>();
    75.         public void AddDocument(TDocument doc)
    76.         {
    77.             lock (this)
    78.             {
    79.                 documentQueue.Enqueue(doc);
    80.             }
    81.         }
    82.         public bool IsDocumentAvailable
    83.         {
    84.             get { return documentQueue.Count > 0; }
    85.         }
    86.         public TDocument GetDocument()
    87.         {
    88.             TDocument doc = default(TDocument);
    89.             lock (this)
    90.             {
    91.                 doc = documentQueue.Dequeue();
    92.             }
    93.             return doc;
    94.         }
    95.         public void DisplayAllDocuments()
    96.         {
    97.             //例用了约束后,不再需要转型输出
    98.             foreach (TDocument doc in this.documentQueue)
    99.             {
    100.                 Console.WriteLine(doc.Title);
    101.                 Console.WriteLine(doc.Content);
    102.                 Console.WriteLine("-----------------------------");
    103.             }
    104.         }
    105.     }
    106. }