《委托和事》PPT课件.ppt
《《委托和事》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《委托和事》PPT课件.ppt(23页珍藏版)》请在三一办公上搜索。
1、1,第6章 委托和事件,委托的概念,面向对象的核心思想之一就是将数据和对数据的操作封装为一个整体。委托变量可以看成是一种安全的函数指针,它只能接受符合其要求的函数地址。函数传递的是数据对象委托(delegate)传递的是函数本身,委托和方法,3,委托封装方法委托类型定义,delegate void DualFunction(int x,int y);public static void Add(int x,int y)Console.WriteLine(0+1=2,x,y,x+y);,与要绑定的方法原型一致,委托和方法,4,委托封装方法委托类型定义委托对象创建,delegate void Du
2、alFunction(int x,int y);public static void Add(int x,int y)Console.WriteLine(0+1=2,x,y,x+y);public static void Main()DualFunction fun1=new DualFunction(Add);,委托和方法,5,委托封装方法委托类型定义委托对象创建调用方法,delegate void DualFunction(int x,int y);public static void Add(int x,int y)Console.WriteLine(0+1=2,x,y,x+y);pub
3、lic static void Main()DualFunction fun1=new DualFunction(Add);Add(1,2);fun1(1,2);,与一般方法调用格式相同,委托和方法,6,委托封装方法,DualFunction fun1=new DualFunction(Add);,委托和方法,7,委托封装方法,DualFunction fun1=new DualFunction(Add);fun1(2,3);,委托和方法,8,委托封装方法,DualFunction fun1=new DualFunction(Add);fun1(2,3);fun1=new DualFuncti
4、on(Sub);,委托和方法,9,委托加减,DualFunction fun1=new DualFunction(Add);fun1(2,3);fun1+=new DualFunction(Mul);,委托和方法,10,委托加减,DualFunction fun1=new DualFunction(Add);fun1(2,3);fun1+=new DualFunction(Mul);fun1(2,3);,委托和方法,11,传递委托对象作为方法参数作为方法返回值,委托和方法,12,Delegate类型DynamicInvokeFun1(2.5,2)Fun1.DynamicInvoke(2.5,2
5、)Combine/RemoveDualFunction fun3=fun1+fun2DualFunction fun3=(DualFunction)(Delegate.Combine(fun1,fun2);,匿名方法,13,委托调用方法命名方法匿名方法,public static void Add(int x,int y)Console.WriteLine(0+1=2,x,y,x+y);.DualFunction fun1=new DualFunction(Add);,DualFunction fun1=delegate(int x,int y)Console.WriteLine(0+1=2,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 委托和事 委托 PPT 课件

链接地址:https://www.31ppt.com/p-5491187.html