C#泛型(四)
来源:互联网 发布:sql server 2008怎么用 编辑:程序博客网 时间:2024/06/06 02:40
六、泛型接口
不论是为泛型容器类,还是表示容器中元素的泛型类,定义接口是很有用的。把泛型接口与泛型类结合使用是更好的用法,比如用IComparable而非IComparable,以避免值类型上的装箱和拆箱操作。.NET框架2.0类库定义了几个新的泛型接口,以配合System.Collections.Generic中新容器类的使用。
当一个接口被指定为类型参数的约束时,只有实现该接口的类型可被用作类型参数。下面的示例代码显示了一个从MyList派生的SortedList类。更多信息,请参见泛型概述。SortedList增加了约束where T : IComparable。
这使得SortedList中的BubbleSort方法可以使用表中的元素的IComparable.CompareTo方法。在这个例子中,表中的元素是简单类——实现IComparable的Person类。
using System;using System.Collections.Generic;//Type parameter T in angle brackets.public class MyList<T>{ protected Node head; protected Node current = null;// Nested type is also generic on T protected class Node { public Node next;//T as private member datatype. private T data; //T used in non-generic constructor. public Node(T t) { next = null; data = t; } public Node Next { get { return next; } set { next = value; } }//T as return type of property. public T Data { get { return data; } set { data = value; } } } public MyList() { head = null; }//T as method parameter type. public void AddHead(T t) { Node n = new Node(t); n.Next = head; head = n; } // Implement IEnumerator<T> to enable foreach // iteration of our list. Note that in C# 2.0 // you are not required to implment Current and // GetNext. The compiler does that for you. public IEnumerator<T> GetEnumerator() { Node current = head; while (current != null) { yield return current.Data; current = current.Next; } }}public class SortedList<T> : MyList<T> where T : IComparable<T>{ // A simple, unoptimized sort algorithm that // orders list elements from lowest to highest:public void BubbleSort() { if (null == head || null == head.Next) return; bool swapped; do { Node previous = null; Node current = head; swapped = false; while (current.next != null) { // Because we need to call this method, the SortedList // class is constrained on IEnumerable<T> if (current.Data.CompareTo(current.next.Data) > 0) { Node tmp = current.next; current.next = current.next.next; tmp.next = current; if (previous == null) { head = tmp; } else { previous.next = tmp; } previous = tmp; swapped = true; } else { previous = current; current = current.next; } }// end while } while (swapped); }}// A simple class that implements IComparable<T>// using itself as the type argument. This is a// common design pattern in objects that are// stored in generic lists.public class Person : IComparable<Person>{ string name; int age; public Person(string s, int i) { name = s; age = i; } // This will cause list elements // to be sorted on age values. public int CompareTo(Person p) { return age - p.age; } public override string ToString() { return name + ":" + age; } // Must implement Equals. public bool Equals(Person p) { return (this.age == p.age); }}class Program{ static void Main(string[] args) { //Declare and instantiate a new generic SortedList class. //Person is the type argument. SortedList<Person> list = new SortedList<Person>(); //Create name and age values to initialize Person objects. string[] names = new string[]{"Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul"}; int[] ages = new int[]{45, 19, 28, 23, 18, 9, 108, 72, 30, 35}; //Populate the list. for (int x = 0; x < 10; x++) { list.AddHead(new Person(names[x], ages[x])); } //Print out unsorted list. foreach (Person p in list) { Console.WriteLine(p.ToString()); } //Sort the list. list.BubbleSort(); //Print out sorted list. foreach (Person p in list) { Console.WriteLine(p.ToString()); } Console.WriteLine("Done"); }}可以在一个类型指定多个接口作为约束,如下:class Stack<T> where T : IComparable<T>, IMyStack1<T>{} 一个接口可以定义多个类型参数,如下:IDictionary<K,V>接口和类的继承规则相同://Okay.IMyInterface : IBaseInterface<int>//Okay.IMyInterface<T> : IBaseInterface<T>//Okay.IMyInterface<T>: IBaseInterface<int>//Error.IMyInterface<T> : IBaseInterface2<T, U>具体类可以实现封闭构造接口,如下:class MyClass : IBaseInterface<string>泛型类可以实现泛型接口或封闭构造接口,只要类的参数列表提供了接口需要的所有参数,如下://Okay.class MyClass<T> : IBaseInterface<T>//Okay.class MyClass<T> : IBaseInterface<T, string>
泛型类、泛型结构,泛型接口都具有同样方法重载的规则。详细信息,请参见泛型方法。
0 0
- C#泛型(四)
- C# C# Attribute特性 (四)
- C# 2.0 Specification (四)
- C#速成(之四)
- C#学习笔记四
- c# 接口(四)
- C#速成(之四)
- C# 2.0 Specification (四)
- C#学习笔记(四)
- C#基础四
- C#学习笔记(四)
- C# Redis实战(四)
- C#多线程(四)
- C#多线程(四)
- c#数字图像处理<四>
- C#基础知识之四
- C#多线程(四)
- C# Redis实战(四)
- TCP/IP网络编程------理论基础
- Android-adb命令大全
- maven学习笔记2
- 1)mysql 安装,数据库创建,表创建
- Go Template学习笔记
- C#泛型(四)
- Android SO 加壳(加密)与脱壳思路
- Android的尺寸单位:dp/sp/density
- iOS10新特性
- ShrewSoft VPN Client使用Cisco VPN Client的pcf文件连接VPN时的2个小问题和解决办法
- 算法篇-3-动态规划-矩阵连乘&最长公共子序列&最大字段和
- Android开发自学笔记(Android Studio)—4.5 ProgressBar及其子类
- 3DGIS第六章 北京奥林匹克公园三维场景网络发布应用
- Android Studio主题设置