zoukankan      html  css  js  c++  java
  • 匿名委托进阶

       上面文章介绍了委托、匿名的定义,下面的例子则是由繁入简,废话不多说,直接上代码:
    class Program { private delegate int sum(int x, int y); private delegate int MyMulticastDelegate(int x, int y); static void Main(string[] args) { Console.WriteLine("委托方式一加法:"); sum addsum = new sum(add); int y = addsum(2, 3); Console.WriteLine("委托方式一加法:2+3" + y); Console.WriteLine("多波委托:有的时候,我们想要调用一个委托,但同时可以执行多个方法(自定义事件中最为常见),比如,一个工作文档生成之后,系统要将生成文档日志,而且还要被保存到数据库中,对于以上二个操作,如果只想调用一个委托,就可以顺序完成,那么使用多播委托,就可以实现。"); MyMulticastDelegate sumdele = new MyMulticastDelegate(add); MyMulticastDelegate subdele = new MyMulticastDelegate(sub); MyMulticastDelegate multicastDelegate = sumdele+subdele; multicastDelegate(2,2); Console.WriteLine("委托中的匿名方法(未简化):"); sum Nofuntion = new sum(delegate( int x,int i) { return x * i; }); int k = Nofuntion(2,3); Console.WriteLine(k); Console.WriteLine("委托中的匿名方法(简化):"); sum _sum =(int x, int m)=>{ return x * m; }; int z = Nofuntion(2, 3); Console.WriteLine(z); Console.ReadKey(); } private static int add(int x, int y) { Console.WriteLine(x+y); return x + y; } private static int sub(int x, int y) { Console.WriteLine(x - y); return x - y; } private static void mulitey(int x, int y) { Console.WriteLine(x * y); } }

      

  • 相关阅读:
    hadoop与spark的处理技巧(六)聚类算法(3)LDA
    hadoop与spark的处理技巧(一)Top N处理技巧
    从零开始学Python 三(网络爬虫)
    Could not get JDBC Connection--java
    idea函数被调用
    人工智能-我们应该了解什么(一)
    从零开始学Python 二
    从零开始学Python 一
    java8 简便的map和list操作
    Could not autowire. No beans of 'xxxx' type found的错误
  • 原文地址:https://www.cnblogs.com/CarzySunshine/p/13878789.html
Copyright © 2011-2022 走看看