泛型接口(C# 编程)
来源:互联网 发布:淘宝炉石金币号怎么买 编辑:程序博客网 时间:2024/06/05 07:43
原文链接:http://blog.csdn.net/u013108312/article/details/52051204
为泛型集合类或表示集合中项的泛型类定义接口通常很有用。对于泛型类,使用泛型接口十分可取,例如使用 IComparable 而不使用 IComparable,这样可以避免值类型的装箱和取消装箱操作。.NET Framework 类库定义了若干泛型接口,以用于 System.Collections.Generic 命名空间中的集合类。
将接口指定为类型参数的约束时,只能使用实现此接口的类型。下面的代码示例显示从 SortedList 类派生的 GenericList 类。有关更多信息,请参见 泛型介绍(C# 编程指南)。 SortedList 添加约束 where T : IComparable。这将使 SortedList 中的 BubbleSort 方法能够对列表元素使用泛型 CompareTo 方法。在此示例中,列表元素为简单类,即实现 Person 的 IComparable。
//Type parameter T in angle brackets.public class GenericList<T> : System.Collections.Generic.IEnumerable<T>{ protected Node head; protected Node current = null; // Nested class is also generic on T protected class Node { public Node next; private T data; //T as private member datatype public Node(T t) //T used in non-generic constructor { next = null; data = t; } public Node Next { get { return next; } set { next = value; } } public T Data //T as return type of property { get { return data; } set { data = value; } } } public GenericList() //constructor { head = null; } public void AddHead(T t) //T as method parameter type { Node n = new Node(t); n.Next = head; head = n; } // Implementation of the iterator public System.Collections.Generic.IEnumerator<T> GetEnumerator() { Node current = head; while (current != null) { yield return current.Data; current = current.Next; } } // IEnumerable<T> inherits from IEnumerable, therefore this class // must implement both the generic and non-generic versions of // GetEnumerator. In most cases, the non-generic method can // simply call the generic method. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }}public class SortedList<T> : GenericList<T> where T : System.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; } } } 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 : System.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() { //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) { System.Console.WriteLine(p.ToString()); } System.Console.WriteLine("Done with unsorted list"); //Sort the list. list.BubbleSort(); //Print out sorted list. foreach (Person p in list) { System.Console.WriteLine(p.ToString()); } System.Console.WriteLine("Done with sorted list"); }}
可将多重接口指定为单个类型上的约束,如下所示:
class Stack<T> where T : System.IComparable<T>, IEnumerable<T>{}
一个接口可定义多个类型参数,如下所示:
interface IDictionary<K, V>{}
适用于类的继承规则同样适用于接口:
interface IMonth<T> { }interface IJanuary : IMonth<int> { } //No errorinterface IFebruary<T> : IMonth<int> { } //No errorinterface IMarch<T> : IMonth<T> { } //No error//interface IApril<T> : IMonth<T, U> {} //Error
如果泛型接口为逆变的,即仅使用其类型参数作为返回值,则此泛型接口可以从非泛型接口继承。在 .NET Framework 类库中,IEnumerable 从 IEnumerable 继承,因为 IEnumerable 只在 GetEnumerator 的返回值和 Current 属性 getter 中使用 T。
具体类可以实现已关闭的构造接口,如下所示:
interface IBaseInterface<T> { }class SampleClass : IBaseInterface<string> { }
只要类参数列表提供了接口必需的所有参数,泛型类便可以实现泛型接口或已关闭的构造接口,如下所示:
interface IBaseInterface1<T> { }interface IBaseInterface2<T, U> { }class SampleClass1<T> : IBaseInterface1<T> { } //No errorclass SampleClass2<T> : IBaseInterface2<T, string> { } //No error
对于泛型类、泛型结构或泛型接口中的方法,控制方法重载的规则相同。
原文链接:http://blog.csdn.net/u013108312/article/details/52051204
- 泛型接口(C# 编程指南)
- 泛型接口(C# 编程指南)
- 泛型接口(C# 编程)
- 3.泛型接口-C#高级编程
- 接口(C# 编程指南)
- C#接口编程(一)
- C#接口编程(二)
- 接口(C# 编程指南)
- C#高级编程二十九天----泛型接口
- 接口属性(C# 编程指南)
- (转帖)C#编程利器:接口(Interface)
- C#接口(interface)知识及编程
- c#面向接口编程
- C#编程之接口
- c#泛型接口
- 全面剖析C#接口编程之实现接口 (1)
- 全面剖析C#接口编程之实现接口 (2)
- C#接口编程之接口概述
- CS231n 学习笔记(2)——神经网络 part2 :Softmax classifier
- UVA11192 Group Reverse
- HP-UX小机故障检测之LOG篇
- java发送http请求,解析html返回的技术
- [leetcode] 19. Remove Nth Node From End of List
- 泛型接口(C# 编程)
- 就是现在
- HP-UX 使用SAM进行Printer设定
- 2016MUTC3-1007 Explorer Bo
- Java中String、StringBuffer和StringBuilder
- 泛型方法(C# 编程)
- 21编码问题
- 22编码问题
- Android中大图片加载及快速浏览处理