日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

泛型--定制泛型接口、泛型类--介绍篇

發布時間:2024/6/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 泛型--定制泛型接口、泛型类--介绍篇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??????泛型類的定義類似于一般的類,只是要使用泛型類型聲明。之后就可以在類中把泛型類型用作成員字段,或方法的參數類型。在定義泛型類時,可以對客戶端代碼能夠在實例化類時用于類型參數的類型種類施加限制。如果客戶端代碼嘗試使用某個約束所不允許的類型來實例化類,則會產生編譯時錯誤。這些限制稱為約束。約束是使用 where 關鍵字指定的。

約束

說明

T:結構

類型參數必須是值類型。可以指定除 Nullable 以外的任何值類型。

T:類

類型參數必須是引用類型;這一點也適用于任何類、接口、委托或數組類型。

T:new()

類型參數必須具有無參數的公共構造函數。當與其他約束一起使用時,new() 約束必須最后指定。

T:<基類名>

類型參數必須是指定的基類或派生自指定的基類。

T:<接口名稱>

類型參數必須是指定的接口或實現指定的接口。可以指定多個接口約束。約束接口也可以是泛型的。

T:U

為 T 提供的類型參數必須是為 U 提供的參數或派生自為 U 提供的參數。這稱為裸類型約束。

簡單約束說明實例:
  • ??? //值類型struct|引用類型class約束
  • ??? public struct Text
  • ??? {
  • ??? }
  • ??? public class Test<T>
  • ??????? where T : struct
  • ??? {
  • ??????? //T在這里是一個值類型
  • ??? }
  • ??? public class Check
  • ??? {
  • ??????? Test<Text> test = new Test<Text>();
  • ??? }
  • ??? //new()約束
  • ??? public class Text
  • ??? {
  • ??????? public Text() { }//無參數的構造函數
  • ??? }
  • ??? public class Test<T>
  • ??????? where T : new()
  • ??? {
  • ??????? //可以在其中使用T t = new T();
  • ??? }
  • ??? public class Check
  • ??? {
  • ??????? Test<Text> test = new Test<Text>();
  • ??? }
  • ??? //基類約束
  • ??? public class Text
  • ??? {
  • ??????? public void Add() { }
  • ??? }
  • ??? public class Test<T>
  • ??????? where T : Text//T繼承至Text
  • ??? {
  • ??????? //可以在類型為T的變量上調用Add()方法
  • ??? }
  • ??? //接口約束見下面實例
  • 自定義泛型實例:
  • //源代碼下載路徑:http://media.wiley.com/product_ancillary/41/07645753/DOWNLOAD/Ch10.zip
  • namespace Wrox.ProCSharp.Generics
  • {
  • ??? using System;
  • ??? using System.Threading;
  • ??? using System.Collections.Generic;
  • ??? public interface IDocument
  • ??? {
  • ??????? string Title { get;}
  • ??????? string Content { get;}
  • ??? }
  • ??? public class Document : IDocument
  • ??? {
  • ??????? private string title;
  • ??????? public string Title
  • ??????? {
  • ??????????? get { return title; }
  • ??????? }
  • ??????? private string content;
  • ??????? public string Content
  • ??????? {
  • ??????????? get { return content; }
  • ??????? }
  • ??????? public Document(string title, string content)
  • ??????? {
  • ??????????? this.title = title;
  • ??????????? this.content = content;
  • ??????? }
  • ??? }
  • ??? /// <summary>
  • ??? /// 定制泛型類【使用接口約束】
  • ??? /// </summary>
  • ??? public class ProcessDocuments<TDocument, TDocumentManager>
  • ??????? where TDocument : IDocument
  • ??????? where TDocumentManager : IDocumentManager<TDocument>
  • ??? {
  • ??????? public static void Start(TDocumentManager dm)
  • ??????? {
  • ??????????? new Thread(new ProcessDocuments<TDocument, TDocumentManager>(dm).Run).Start();
  • ??????? }
  • ??????? protected ProcessDocuments(TDocumentManager dm)
  • ??????? {
  • ??????????? //注意不能把null賦予泛型類型,因為泛型類型也可以是值類型。
  • ??????????? //其中T doc = default(T);//則會將null賦予引用類型,把0賦予值類型。
  • ??????????? documentManager = dm;
  • ??????? }
  • ??????? //使用泛型類型定義成員字段
  • ??????? private TDocumentManager documentManager;
  • ??????? protected void Run()
  • ??????? {
  • ??????????? while (true)
  • ??????????? {
  • ??????????????? if (documentManager.IsDocumentAvailable)
  • ??????????????? {
  • ??????????????????? TDocument doc = documentManager.GetDocument();
  • ??????????????????? Console.WriteLine("Processing document {0}", doc.Title);
  • ??????????????? }
  • ??????????????? Thread.Sleep(20);
  • ?? ?????????}
  • ??????? }
  • ??? }
  • ??? /// <summary>
  • ??? /// 定制泛型接口
  • ??? /// </summary>
  • ??? public interface IDocumentManager<T>
  • ??? {
  • ??????? void AddDocument(T doc);
  • ??????? T GetDocument();
  • ??????? bool IsDocumentAvailable
  • ??????? {
  • ??????????? get;
  • ??????? }
  • ??? }
  • ??? /// <summary>
  • ??? /// 定制泛型類
  • ??? /// </summary>
  • ??? public class DocumentManager<T> : IDocumentManager<T>
  • ??? {
  • ??????? private Queue<T> documentQueue = new Queue<T>();
  • ??????? //泛型類型作為參數類型
  • ??????? public void AddDocument(T doc)
  • ??????? {
  • ??????????? lock (this)
  • ??????????? {
  • ??????????????? documentQueue.Enqueue(doc);
  • ??????????? }
  • ??????? }
  • ??????? //泛型類型作為返回類型
  • ??????? public T GetDocument()
  • ??????? {
  • ??????????? T doc;
  • ??????????? lock (this)
  • ??????????? {
  • ??????????????? doc = documentQueue.Dequeue();
  • ??????????? }
  • ??????????? return doc;
  • ??????? }
  • ??????? public bool IsDocumentAvailable
  • ??????? {
  • ??????????? get { return (documentQueue.Count > 0) ? true : false; }
  • ??????? }
  • ??? }
  • ??? class Program
  • ??? {
  • ??????? static void Main(string[] args)
  • ??????? {
  • ??????????? DocumentManager<Document> dm = new DocumentManager<Document>();
  • ??????????? ProcessDocuments<Document, DocumentManager<Document>>.Start(dm);
  • ??????????? for (int i = 0; i < 1000; i++)
  • ??????? ????{
  • ??????????????? Document doc = new Document("title" + i.ToString(), "content");
  • ??????????????? dm.AddDocument(doc);
  • ??????????????? Console.WriteLine("added document {0}", doc.Title);
  • ??????????????? Thread.Sleep(10);
  • ??????????? }
  • ??????? }
  • ??? }
  • }
  • ?

    轉載于:https://www.cnblogs.com/swollaws/archive/2009/05/12/1455115.html

    總結

    以上是生活随笔為你收集整理的泛型--定制泛型接口、泛型类--介绍篇的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。