友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
热门书库 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

c#高级编程(第6版)--第九章-第2章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




last = first;



else



last。Next = newNode;

last = newNode;



return newNode;



public IEnumerator GetEnumerator()



LinkedListNode current = first;

while (current != null)



yield return current。Value;

current = current。Next;







现在可以给任意类型使用LinkedList类了。在下面的代码中,实例化了一个新LinkedList对象,添加了两个整数类型和一个字符串类型。整数类型要转换为一个对象,所以执行装箱操作,如前面所述。在foreach语句中执行拆箱操作。在foreach语句中,链表中的元素被强制转换为整数,所以对于链表中的第三个元素,会发生一个运行异常,因为它转换为int时会失败。

LinkedList list1 = new LinkedList();

list1。AddLast(2);

list1。AddLast(4);

list1。AddLast(〃6〃);

foreach (int i in list1)



Console。WriteLine(i);



下面创建链表的泛型版本。泛型类的定义与一般类类似,只是要使用泛型类型声明。之后,泛型类型就可以在类中用作一个字段成员,或者方法的参数类型。LinkedListNode类用一个泛型类型T声明。字段value的类型是T,而不是object。构造函数和Value属性也变为接受和返回T类型的对象。也可以返回和设置泛型类型,所以属性Next和Prev的类型是LinkedListNode。

public class LinkedListNode



private T value;

public LinkedListNode(T value)



this。value = value;



public T Value



get { return value; }



private LinkedListNode next;

public LinkedListNode Next



get { return next; }

internal set { next = value; }



private LinkedListNode prev;

public LinkedListNode Prev



get { return prev; }

internal set { prev = value; }





下面的代码把LinkedList类也改为泛型类。LinkedList包含LinkedListNode元素。LinkedList中的类型T定义了类型T的包含字段first和last。AddLast()方法现在接受类型T的参数,实例化LinkedListNode类型的对象。

IEnumerable接口也有一个泛型版本IEnumerable。IEnumerable派生于IEnumerable,添加了返回IEnumerator的GetEnumerator()方法,LinkedList执行泛型接口IEnumerable。

提示:

枚举、接口IEnumerable和IEnumerator详见第5章。

public class LinkedList : IEnumerable



private LinkedListNode first;

public LinkedListNode First



get { return first; }



private LinkedListNode last;

public LinkedListNode Last



get { return last; }



public LinkedListNode AddLast(T node)



LinkedListNode newNode = new LinkedListNode(node);

if (first  null)



first = newNode;

last = first;



else



last。Next = newNode;

last = newNode;



return newNode;



public IEnumerator GetEnumerator()



LinkedListNode current = first;

while (current != null)



yield return current。Value;

current = current。Next;





IEnumerator IEnumerable。GetEnumerator()



return GetEnumerator();





使用泛型类LinkedList,可以用int类型实例化它,且无需装箱操作。如果不使用AddLast()方法传送int,就会出现一个编译错误。使用泛型IEnumerable,foreach语句也是类型安全的,如果foreach语句中的变量不是int,也会出现一个编译错误。

LinkedList list2 = new LinkedList();

list2。AddLast(1);

list2。AddLast(3);

list2。AddLast(5);

foreach (int i in list2)



Console。WriteLine(i);



同样,可以给泛型LinkedList使用string类型,将字符串传送给AddLast()方法。

LinkedList list3 = new LinkedList();

list3。AddLast(〃2〃);

list3。AddLast(〃four〃);

list3。AddLast(〃foo〃);

foreach (string s in list3)



Console。WriteLine(s);



提示:

每个处理对象类型的类都可以有泛型实现方式。另外,如果类使用了继承,泛型非常有助于去除类型转换操作。

9。3  泛型类的特性

在创建泛型类时,需要一些其他C#关键字。例如,不能把null赋予泛型类型。此时,可以使用default关键字。如果泛型类型不需要Object类的功能,但需要调用泛型类上的某些特定方法,就可以定义约束。

本节讨论如下主题:

●       默认值

●       约束

●       继承

●       静态成员

下面开始一个使用泛型文档管理器的示例。文档管理器用于从队列中读写文档。先创建一个新的控制台项目DocumentManager,添加类DocumentManager。AddDocument()方法将一个文档添加到队列中。如果队列不为空,IsDocumentAvailable只读属性就返回true。

using System;

using Systemllections。Generic;

namespace Wrox。ProCSharp。Generics



public class DocumentManager



private readonly Queue documentQueue = new Queue();

public void AddDocument(T doc)



lock (this)



documentQueue。Enqueue(doc);





public bool IsDocumentAvailable



get { return documentQueueunt 》 0; }






9。3。1  默认值

现在给DocumentManager类添加一个GetDocument()方法。在这个方法中,给类型T指定null。但是,不能把null赋予泛型类型。原因是泛型类型也可以实例化为值类型,而null只能用于引用类型。为了解决这个问题,可以使用default关键字。通过default关键字,将null赋予引用类型,将0赋予值类型。

public T GetDocument()



T doc = default(T);

lock (this)



doc = documentQueue。Dequeue();



return doc;



注意:

default关键字根据上下文可以有多种含义。switch语句使用default定义默认情况。在泛型中,根据泛型类型是引用类型还是值类型,default关键字用于将泛型类型初始化为null或0。
9。3。2  约束

如果泛型类需要调用泛型类型上的方法,就必须添加约束。对于DocumentManager,文档的标题应在DisplayAllDocuments()方法中显示。

Document类执行带有Title和Content属性的IDocument接口:

public interface IDocument



string Title { get; set; }

string Content { get; set; }



public class Document : IDocument



public Document()





public Document(string title; string content)



this。title = title;

thisntent = content;



public string Title { get; set; }

public string Content { get; set; }



要使用DocumentManager类显示文档,可以将类型T强制转换为IDocument接口,以显示标题:

public void DisplayAllDocuments()



foreach (T doc in documentQueue)



Console。WriteLine((IDocument)doc)。Title);





问题是,如果类型T没有执行IDocument接口,这个类型转换就会生成一个运行异常。最好给DocumentManager类定义一个约束:TDocument类型必须执行IDocument接口。为了在泛型类型的名称中指定该要求,将T改为TDocument。where子句指定了执行IDocument接口的要求。

public class DocumentManager

where TDocument : IDocument



这样,就可以编写foreach语句,让类型T包含属性Title了。Visual Studio IntelliSense和编译器都会提供这个支持。

public void DisplayAllDocuments()



foreach (TDocument doc in documentQueue)



Console。WriteLine(doc。Title);





在Main()方法中,DocumentManager类用Document类型实例化,而Document类型执行了需要的IDocument接口。接着添加和显示新文档,检索其中一个文档:

static void Main()



DocumentManager dm = new DocumentManager();

dm。AddDocument(new Document(〃Title A〃; 〃Sample A〃));

dm。AddDocument(new Document(〃Title B〃; 〃Sample B〃));

dm。DisplayAllDocuments();

if (dm。IsDocumentAvailable)



Document d = dm。GetDocument();

Console。WriteLine(dntent);





DocumentManager现在可以处理任何执行了IDocument接口的类。

在示例应用程序中,介绍了接口约束。泛型还有几种约束类型,如表9…1所示。

 

表  9…1

约    束


说    明

where T : struct


使用结构约束,类型T必须是值类型

where T : class


类约束指定,类型T必须是引用类型

where T : IFo
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!