C#中泛型特性2-约束
来源:互联网 发布:阿里云实名认证 编辑:程序博客网 时间:2024/06/05 06:20
- 让我们先看看不用约束的代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 泛型
- {
- class 泛型的特性
- {
- public static void Main()
- {
- DocumentManager<IDocument> manager = new DocumentManager<IDocument>();
- manager.AddDocument( new Document( "aladdin" , "aladdin is a super man") );
- manager.AddDocument(new Document("zhao", "jacky is a super man"));
- manager.AddDocument(new Document("jacky", "jacky is a super man"));
- manager.DisplayAllDocuments();
- Console.ReadLine();
- }
- }
- interface IDocument
- {
- string Title { get; set; }
- string Content { get;set;}
- }
- public class Document : IDocument
- {
- private string title;
- private string content;
- public Document(string title, string content)
- {
- this.title = title;
- this.content = content;
- }
- public string Title
- {
- get
- {
- return this.title;
- }
- set
- {
- this.title = value;
- }
- }
- public string Content
- {
- get
- {
- return this.content;
- }
- set
- {
- this.content = value;
- }
- }
- }
- class DocumentManager<T>
- {
- //表示对象先进先出的集合
- private readonly Queue<T> documentQueue = new Queue<T>();
- public void AddDocument(T doc)
- {
- lock (this)
- {
- documentQueue.Enqueue(doc);
- }
- }
- public bool IsDocumentAvailable
- {
- get { return documentQueue.Count > 0; }
- }
- public T GetDocument()
- {
- T doc = default(T);
- lock (this)
- {
- doc = documentQueue.Dequeue();
- }
- return doc;
- }
- //如果要显示全部的文档,跌代输出,在foreach中则要进行转型
- public void DisplayAllDocuments()
- {
- //些处没有使用约束,如果T类型没有在客户代码中定义为Idocument接口,则会出现异常
- foreach (T doc in this.documentQueue)
- {
- Console.WriteLine( ((IDocument)doc).Title );
- Console.WriteLine(((IDocument)doc).Content);
- Console.WriteLine( "---------------------------");
- }
- }
- }
- }
- ---------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 泛型
- {
- class 泛型的特性
- {
- public static void Main()
- {
- //例用泛型约束后,只能使用指定的类型为类实例化
- DocumentManager<IDocument> manager = new DocumentManager<IDocument>();
- manager.AddDocument(new Document("aladdin", "aladdin is a super man"));
- manager.AddDocument(new Document("zhao", "jacky is a super man"));
- manager.AddDocument(new Document("jacky", "jacky is a super man"));
- manager.DisplayAllDocuments();
- while (manager.IsDocumentAvailable)
- {
- IDocument doc = manager.GetDocument() ;
- Console.WriteLine( doc.Title );
- Console.WriteLine(doc.Content);
- }
- Console.ReadLine();
- }
- }
- interface IDocument
- {
- string Title { get; set; }
- string Content { get; set; }
- }
- public class Document : IDocument
- {
- private string title;
- private string content;
- public Document(string title, string content)
- {
- this.title = title;
- this.content = content;
- }
- public string Title
- {
- get
- {
- return this.title;
- }
- set
- {
- this.title = value;
- }
- }
- public string Content
- {
- get
- {
- return this.content;
- }
- set
- {
- this.content = value;
- }
- }
- }
- //指定TDocument类型执行的接口是IDocument
- //泛型还有好几个约束方式
- //1 where T : struct 使用结构约束,类型必须是值类型
- //2 where T : class 使用类约束,T类型必须是引用类型
- //3 where T :IFoo(接口) 类型必须引用接口(我们就是用的这种)
- //4 where T : Foo(类) 类型必须是集成这个类
- //5 where T : new() 这是一个构造函数约束,T类型必须拥有一个默认构造函数
- //6 where T : U 指定类型派生于另一个泛型类型,这叫做 裸类型约束
- class DocumentManager<TDocument> where TDocument : IDocument
- {
- //表示对象先进先出的集合
- private readonly Queue<TDocument> documentQueue = new Queue<TDocument>();
- public void AddDocument(TDocument doc)
- {
- lock (this)
- {
- documentQueue.Enqueue(doc);
- }
- }
- public bool IsDocumentAvailable
- {
- get { return documentQueue.Count > 0; }
- }
- public TDocument GetDocument()
- {
- TDocument doc = default(TDocument);
- lock (this)
- {
- doc = documentQueue.Dequeue();
- }
- return doc;
- }
- public void DisplayAllDocuments()
- {
- //例用了约束后,不再需要转型输出
- foreach (TDocument doc in this.documentQueue)
- {
- Console.WriteLine(doc.Title);
- Console.WriteLine(doc.Content);
- Console.WriteLine("-----------------------------");
- }
- }
- }
- }
- C#中泛型特性2-约束
- C#中泛型约束(where)是什么?
- C#约束
- DNA化学特性约束
- Android Studio2.2 的新特性--约束布局
- C#中泛型特性1-默认值
- C# 特性
- C#特性
- c#特性
- C#特性
- C#特性
- C# 特性
- C# 特性
- C# 特性
- C#特性
- c# 特性
- C# 2.0 约束
- C#访问约束问题
- 传值和传引用
- -
- Flex, SilverLight 和 JavaFX
- -
- 图解VirtualBox 的共享文件夹设置
- C#中泛型特性2-约束
- -
- 迪堡ATM Opteva 系列安装验机条系列文章
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Validate Binary Search Tree
- 【LeetCode with Python】 String to Integer (atoi)
- 【LeetCode with Python】 Single Number
- apache 2 安装
- 【LeetCode with Python】 Same Tree