• C#(委托a)


    C#(委托a)
        public delegate double TheOperator(double x, double y);    
            
        public class Operators    
        {    
         static public double Add(double x, double y)    
         {    
          return x + y;    
         }    
            
         static public double Sub(double x, double y)    
         {    
          return x - y;    
         }    
            
         //乘,除法 我也懒得废话了    
        }    
            
        public class DotNet_Calculator    
        {    
         public double Foo(TheOperator op, double x, double y)    
         {    
          return op(x, y);    
         }    
        }    
            
        public class TheMainClass    
        {    
         static public void Main()    
         {    
          TheOperator myAdd = new TheOperator(Operators.Add);    
          TheOperator mySub = new TheOperator(Operators.Sub);    
            
          DotNet_Calculator dc = new DotNet_Calculator();    
          double sum = dc.Foo(myAdd, 2, 4); //sum = 6    
          System.Console.WriteLine(sum);    
          double sub = dc.Foo(mySub, 3, 7); //sub = -4    
          System.Console.WriteLine(sub);    
         }    
        }    
    
    
    
    C#(委托b)
    //小张类
        public class MrZhang
        {
            //其实买车票的悲情人物是小张
            public static void BuyTicket()
            {
                Console.WriteLine("NND,每次都让我去买票,鸡人呀!");
            }
        }
    
        //小明类
        class MrMing
        {
            //声明一个委托,其实就是个“命令”
            public delegate void BugTicketEventHandler();
    
            public static void Main(string[] args)
            {
                //这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”
                BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket);
    
                //这时候委托被附上了具体的方法
                myDelegate();
                Console.ReadKey();
            }
        } 
  • 相关阅读:
    487-3279(电话号码)
    【模板】二分图匹配
    【模板】网络最大流
    P3879 [TJOI2010]阅读理解
    10.10 考试T1 低仿机器人
    P4025 [PA2014]Bohater
    线段树合并 从入门到入土
    CF547B Mike and Feet
    10.6洛谷月赛划水记
    P4552 [Poetize6] IncDec Sequence
  • 原文地址:https://www.cnblogs.com/yangwujun/p/3405996.html
Copyright © 2020-2023  润新知