源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

浅谈c# 泛型类的应用

  • 时间:2020-09-28 05:32 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:浅谈c# 泛型类的应用
[b]泛型类 [/b]泛型类封装不是特定于具体数据类型的操作。 泛型类最常用于集合,如链接列表、哈希表、堆栈、队列、树等。 像从集合中添加和移除项这样的操作都以大体上相同的方式执行,与所存储数据的类型无关。对大多集合类的操作,推荐使用 .NET Framework 类库中所提供的类。 [b](1)泛型类可以继承具体类、封闭式构造、开放式构造基类。 [/b]
[u]复制代码[/u] 代码如下:
class BaseNode { } class BaseNodeGeneric<T> { } // 继承具体类 class NodeConcrete<T> : BaseNode { } //继承封闭式构造基类 //封闭式构造基类指基类类型参数指定具体类型 class NodeClosed<T> : BaseNodeGeneric<int> { } //继承开放式构造基类 //开放式构造基类指基类类型参数未指定 class NodeOpen<T> : BaseNodeGeneric<T> { }
[b](2)基类类型参数必须在子类中指定实现。 [/b]
[u]复制代码[/u] 代码如下:
//正确 class Node1 : BaseNodeGeneric<int> { } //错误 //在子类中未指定父类类型参数实现 class Node2 : BaseNodeGeneric<T> {} //错误 //在子类中未指定父类类型参数实现 class Node3 : T {} class BaseNodeMultiple<T, U> { } //正确 class Node4<T> : BaseNodeMultiple<T, int> { } //正确 class Node5<T, U> : BaseNodeMultiple<T, U> { } //错误 //在子类中未指定父类类型参数实现 class Node6<T> : BaseNodeMultiple<T, U> {}
[b](3)从开放式构造类型继承的泛型类必须指定约束,这些约束是基类型约束的超集或暗示基类型约束。 [/b]
[u]复制代码[/u] 代码如下:
class NodeItem<T> where T : System.IComparable<T>, new() { } class SpecialNodeItem<T> : NodeItem<T> where T : System.IComparable<T>, new() { }
[b](4)泛型类型可以使用多个类型参数和约束。 [/b]
[u]复制代码[/u] 代码如下:
class SuperKeyType<K, V, U>     where U : System.IComparable<U>     where V : new() { }
(5)开放式构造类型和封闭式构造类型可以用作方法参数。
[u]复制代码[/u] 代码如下:
void Swap<T>(List<T> list1, List<T> list2) { } void Swap(List<int> list1, List<int> list2) { }
[b]泛型接口 (1)泛型类型参数可指定多重接口约束。 [/b]
[u]复制代码[/u] 代码如下:
class Stack<T> where T : System.IComparable<T>, IEnumerable<T> { }
[b](2)接口可以定义多个类型参数。 [/b]
[u]复制代码[/u] 代码如下:
interface IDictionary<K, V> { }
[b](3)类继承规则适用接口继承规则。(参考上面泛型类继承) (4)泛型接口实例 [/b]
[u]复制代码[/u] 代码如下:
class GenericInterface     {         static void Main()         {             SortedList<Person> list = new SortedList<Person>();             string[] names = new string[]             {                 "zhang san",                 "li si",                 "wang wu",                 "zhou er",                 "he yi"             };             int[] ages = new int[] { 22, 15, 30, 34, 12 };             for (int x = 0; x < 5; x++)             {                 list.AddNode(new Person(names[x], ages[x]));             }             foreach (Person p in list)             {                 System.Console.WriteLine(p.ToString());             }             Console.WriteLine("------------------排序-----------------------");             list.BublleSort();             foreach (Person p in list)             {                 System.Console.WriteLine(p.ToString());             }             Console.Read();         }     }     public class GenericList<T> : System.Collections.Generic.IEnumerable<T>     {         public class Node         {             private T data;             public T Data             {                 get { return data; }                 set { data = value; }             }             private Node next;             public Node Next             {                 get { return next; }                 set { next = value; }             }             private Node last;             public Node Last             {                 get { return last; }                 set { last = value; }             }             public Node(T t)             {                 data = t;                 next = null;             }         }         public Node firstNode;         private Node lastNode;         public void AddNode(T t)         {             Node node = new Node(t);             node.Last = lastNode;             if (lastNode != null)                 lastNode.Next = node;             lastNode = node;             if (firstNode == null)                 firstNode = node;         }         #region IEnumerable<T> 成员         public IEnumerator<T> GetEnumerator()         {             Node current = firstNode;             while (current != null)             {                 //yield return表达式以枚举对象返回                 yield return current.Data;                 current = current.Next;             }         }         #endregion         #region IEnumerable 成员          //IEnumerable < T >继承自IEnumerable,         //因此这类必须实现泛型和非泛型的版本的GetEnumerator。         //在大多数情况下,非泛型方法简单调用泛型方法。         IEnumerator IEnumerable.GetEnumerator()         {             return GetEnumerator();         }         #endregion     }     public class SortedList<T> : GenericList<T> where T : System.IComparable<T>     {         //该方法实现排序         public void BublleSort()         {             if (firstNode == null || firstNode.Next == null)                 return;             bool swapped;             do             {                 Node last = null;                 Node current = firstNode;                 swapped = false;                 while (current.Next != null)                 {                     if (current.Data.CompareTo(current.Next.Data) > 0)                     {                         /* 当前节点大于下一个节点,位置交换*/                         Node tmp = current.Next;                         current.Next = current.Next.Next;                         tmp.Next = current;                         if (last == null)                         {                             firstNode = tmp;                         }                         else                         {                             last.Next = tmp;                         }                         last = tmp;                         swapped = true;                     }                     else                     {                         last = current;                         current = current.Next;                     }                 }             }             while (swapped);         }     }     public class Person : System.IComparable<Person>     {         string name;         int age;         public Person(string n, int a)         {             name = n;             age = a;         }         #region IComparable<Person> 成员         public int CompareTo(Person p)         {             //按年龄排序             //return age - p.age;             //按名称排序             int a =name.CompareTo(p.name);             return a;         }         #endregion         public override string ToString()         {             return name + ":" + age;         }     }
[b]输出如下: [/b] [img]http://files.jb51.net/file_images/article/201305/2013053111113116.jpg[/img] [b]泛型方法 [/b]包含类型参数声明的方法即为泛型方法。 [b](1)泛型类的类型参数与它内部泛型方法的类型参数一致,编译器将生成警告 CS0693。 [/b]
[u]复制代码[/u] 代码如下:
class GenericList<T> {     // CS0693     void SampleMethod<T>() { } }
[b](2)泛型方法的类型参数可以进行约束。 (3)泛型方法可以使用许多类型参数进行重载。 [/b]
[u]复制代码[/u] 代码如下:
void DoWork() { } void DoWork<T>() { } void DoWork<T, U>() { }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部