C#面向对象程序设计课件郑宇军CS章节11章

上传人:E**** 文档编号:89086003 上传时间:2019-05-17 格式:PPT 页数:31 大小:472KB
返回 下载 相关 举报
C#面向对象程序设计课件郑宇军CS章节11章_第1页
第1页 / 共31页
C#面向对象程序设计课件郑宇军CS章节11章_第2页
第2页 / 共31页
C#面向对象程序设计课件郑宇军CS章节11章_第3页
第3页 / 共31页
C#面向对象程序设计课件郑宇军CS章节11章_第4页
第4页 / 共31页
C#面向对象程序设计课件郑宇军CS章节11章_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《C#面向对象程序设计课件郑宇军CS章节11章》由会员分享,可在线阅读,更多相关《C#面向对象程序设计课件郑宇军CS章节11章(31页珍藏版)》请在金锄头文库上搜索。

1、1,第11章 泛型程序设计,2,为什么要使用泛型,public class IntStack private int items = new string100; private int current = 0; public void Push(int x) itemscurrent+ = x; public int Pop() return itemscurrent-; public static void Main() IntStack st = new IntStack(); for(int i=1; i=10; i+) st.Push(i); int x = st.Pop(); ,3

2、,为什么要使用泛型,public class StringStack private string items = new string100; private int current = 0; public void Push(string x) itemscurrent+ = x; public string Pop() return itemscurrent-; public static void Main() StringStack st = new StringStack(); for(int i=1; i=10; i+) st.Push(new string(a,i); stri

3、ng y = st.Pop(); ,4,为什么要使用泛型,public class DoubleStack public class DateTimeStack public class StudentStack public class TeacherStack ,5,为什么要使用泛型,public class ObjStack private object items = new object100; private int current = 0; public void Push(object x) itemscurrent+ = x; public object Pop() retu

4、rn itemscurrent-; public static void Main() IntStack st1 = new IntStack(); StringStack st2 = new StringStack(); for(int i=1; i=10; i+) st1.Push(i); st2.Push(new string(a,i); int x = (int)st1.Pop(); string y = (string)st2.Pop(); ,装箱,装箱,拆箱,拆箱,6,为什么要使用泛型,public class Stack private T items = new T100; p

5、rivate int current = 0; public void Push(T x) itemscurrent+ = x; public T Pop() return itemscurrent-; public static void Main() Stack st1 = new Stack(); Stack st2 = new Stack(); for(int i=1; i=10; i+) st1.Push(i); st2.Push(new string(a,i); int x = st1.Pop(); string y = st2.Pop(); ,无装箱,无拆箱,7,为什么要使用泛型

6、,泛型的优越性 更高层次上的抽象性 对算法和数据结构更强的表达能力 更为方便的复用:参数替换 类型安全性:无需装箱/拆箱 不损害程序性能而实现更高的灵活性,8,泛型类,声明 类名+类型参数,public class Stack ,9,泛型类,声明 类名+类型参数 定义 使用类型参数指代类型,public class Stack private T items = new T100; private int current = 0; public void Push(T x) itemscurrent+ = x; ,10,泛型类,声明 类名+类型参数 定义 使用类型参数指代类型 使用 类型参数代

7、入具体类型,public class Stack private T items = new T100; private int current = 0; public void Push(T x) itemscurrent+ = x; public static void Main() Stack st1 = new Stack(); for(int i=1; i=10; i+) st1.Push(i); ,11,泛型类,类型参数使用场合 字段和属性类型 方法的参数类型和返回类型 方法代码中的局部变量类型,12,泛型类,多个类型参数,public class Stack public cla

8、ss Stack public class Stack public class Stack ,public class Stack public class Stack public class Stack public class Stack ,13,泛型类,泛型类的静态成员,public class Stack public static int size = 100; T items = new Tsize; public static void Main() Stack.size = 500; Stack.size = 800; ,14,Demo LinkNode,15,类型限制,无

9、限制类型参数T,T t1, t2; Console.Write(t1.ToString(); if (t1.Equals(t2) Console.Write(t1.GetType(); Console.Write(t2.GetHashCode();,T t1 = 0; T t2 = null; if (t1 = t2) Console.Write(typeof(T);,16,类型限制,主要限制 struct: 值类型 class: 引用类型,T t1 = null; T t2 = null; if (t1 = t2) Console.Write(typeof(T);,public class

10、Stack where T : class ,17,类型限制,次要限制 基类限制 接口限制,T t1 = null; int x = t1.GetValue()+; T t2 = t1+;,public class A public int GetValue() public static A operator + (A a) public class Stack where T : A ,18,类型限制,构造函数限制,public class Stack where T : new() ,T t1 = new T();,19,泛型继承,开放类型,R, S, T Queue, Stack Pa

11、ir, Triple Pair, Pair Triple,20,泛型继承,开放类型 封闭类型,int, DateTime string, object Queue, Stack Pair Triple,R, S, T Queue, Stack Pair, Triple Pair, Pair Triple,21,泛型继承,合法继承,public class A public class B: A public class Stack : A public class Pair : Stack public class Pair : Stack public class Triple: Pair

12、,22,泛型继承,非法继承,public class B: Stack public class Pair : Stack public class Pair : Stack public class Pair: Triple ,23,Demo BiLinkNode,24,泛型接口,定义泛型接口 同样只包含方法声明 同样可使用类型参数作为方法参数类型和返回类型 同样可对类型参数进行类型限制,public interface IPrintable void Print(); public interface IIndexable where T: IPrintable T Find(int in

13、dex); ,25,泛型接口,实现泛型接口 普通类实现泛型接口,public interface IIndexable where T: IPrintable T Find(int index); ,public class C1 : IIndexable public int Find(int index) . public class C1 : IIndexable public string Find(int index) . ,26,泛型接口,实现泛型接口 普通类实现泛型接口 泛型类实现泛型接口,public interface IIndexable where T: IPrintab

14、le T Find(int index); ,public class C : IIndexable public int Find(int index) . public class Stack : IIndexable public T Find(int index) . ,27,泛型接口,常用泛型接口 IEnumerator IEnumerable ICollection Ilist IComparable IComparer IDictionary,28,Demo LinkList,29,泛型方法,泛型方法 泛型类的泛型方法,public class CMath static void Swap(ref T x, ref T y) T t=x; x=y; y=t; ,30,泛型方法,泛型方法 泛型类的泛型方法 非泛型类的泛型方法,public class CMath static void Swap(ref T x, ref T y) T t=x; x=y; y=t; ,31,本章习题,类型参数代表什么?如何使用类型参数? 简述泛型类之间继承的基本要求。 简述泛型类与泛型接口之间继承的基本要求。 练习使用.NET类库中的基本泛型类:Stack, Queue, List。,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号