zoukankan      html  css  js  c++  java
  • 委托初步了解(代码段展示)

    一.非含参委托

    using System;
    namespace 委托
    {
        //2 定义委托类型  委托和目标方法基本一致
        public delegate void DelegateEat();
         class Program
        {//1 应该有目标方法
            public static void ZSEat()
            {
                Console.WriteLine("张三吃饭饭--->");
            }
            public static void LSEat( )
            {
                Console.WriteLine("李四吃饭饭--->");
            }       
            public static void Main(string[] args)
            {  //3 申明委托变量
                DelegateEat delegateEat;
                //4 赋值
                delegateEat = ZSEat;
                delegateEat += LSEat;         
                delegateEat += delegateEat;
                delegateEat -= LSEat; 
                //5 执行委托
                delegateEat();
            }
        }   
    }

    结果:张三吃饭饭--->

             李四吃饭饭--->
             张三吃饭饭--->

    二.含参数委托类

    例一:目标函数中放一个参数

    using System;
    namespace ConsoleApplication1
    {    
            public delegate void DelegateEat();//2
            class Program
            {
                /// 第一个参数放置符合类型的方法!!!
                public static void ExcDelegateEat(DelegateEat de)//1
                {              
                    Console.WriteLine("先执行");
                    de();
                }
                public static void LSEat()
                {                
                    Console.WriteLine("赵六吃呵呵哒");              
                }
                public static void Main(string[] args)
                {
                    ExcDelegateEat(LSEat);
                }        
        }
    }

    结果:先执行

    赵六吃呵呵哒

    例二:目标函数中放两个参数

    using System;namespace ConsoleApplication1
    {    
            public delegate void DelegateEat();//2
            class Program
            {
                /// 第一个参数放置符合类型的方法!!!
                public static void ExcDelegateEat(DelegateEat de, string userName)//1
                {              
                    Console.WriteLine("{0}在使用吃的功能", userName);
                    de();
                }
                public static void LSEat()
                {                
                    Console.WriteLine("赵六吃呵呵哒");              
                }
                public static void Main(string[] args)
                {
                    ExcDelegateEat(LSEat, "李四");
                }        
        }
    }

    结果:李四在使用吃的功能

    赵六吃呵呵哒

    例三:目标函数和委托类型均有两个参数

     1 using System;
     2 namespace ConsoleApplication1
     3 {    
     4         public delegate void DelegateEat(string x,string y);//2
     5         class Program
     6         {
     7             public static void ExcDelegateEat(DelegateEat de, string userName)//1
     8             {
     9                 string tr = "王五";
    10                 string er = "洪七";
    11                 Console.WriteLine("{0}在使用吃的功能", userName);
    12                 de(tr,er);
    13             }
    14             public static void LSEat(string m,string n)
    15             {
    16                 Console.WriteLine("{0}吃呵呵哒",m);
    17                 Console.WriteLine("赵六吃呵呵哒");
    18                 Console.WriteLine("{0}吃呵呵哒",n);
    19             }
    20             public static void Main(string[] args)
    21             {
    22                 ExcDelegateEat(LSEat, "李四");
    23                 Console.ReadKey();
    24             }        
    25     }
    26 }

    结果:李四在使用吃的功能

    王五吃呵呵哒

    赵六吃呵呵哒

    洪七吃呵呵哒

     

  • 相关阅读:
    geth搭建以太坊私链及常用操作
    什么是区块链?
    linux的命令
    MySQL的sql优化
    如何防止秒杀抢购超卖?
    try catch影响Spring事务吗?
    mysql中InnoDB与MyISAM的区别
    进程和线程的区别
    Nginx采用yum安装方式及安装后的目录
    Springboot2.0中jpa默认创建的mysql表为myisam引擎问题
  • 原文地址:https://www.cnblogs.com/Future-Better/p/9832417.html
Copyright © 2011-2022 走看看