zoukankan      html  css  js  c++  java
  • 委托,委托链学习

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DelegateTest
    {
    
        delegate void  myDelegateHandle(string s);
        class Program
        {
            static void Main(string[] args)
            {
                myDelegateHandle mydel = new myDelegateHandle(A.Print);
                myDelegateHandle mydelb = new myDelegateHandle(B.Print);
    
                try
                {
                    mydel += mydelb;
                    mydel("aaaaa");
    
                    mydel -= mydelb;
    
    
                    mydel("uuuuuuuuu");
    
                    mydel -= mydel;
    
                    mydel("tttt");
    
                    Console.Read();
                }
                catch (NullReferenceException nullEx)
                {
                    Console.WriteLine("Null"+nullEx.Message);
    
    
                }
                finally
                {
    
                    Console.Read();
                
                }
            }
        }
    
        class A
        {
            public  static void Print(string msg)
            {
    
                Console.WriteLine(msg);
            }
            
        }
    
        class B
        {
            public  static void Print(string msg)
            {
    
                Console.WriteLine("2222222222222");
            }
        }
    
    }
    

    声明委托(定义好返回类型、要传递的参数,这是要与指向的函数相一致的)

    实例化委托,传递要指向的函数名,此时不加“()”;

    直接传递给委托和与已经定义好参数类型一致的参数。

  • 相关阅读:
    (转载)Linux进程基础
    C语言字符串
    DNS域名解析服务
    Linux-SSH远程管理
    Linux文件系统深入了解
    Linux进程和计划任务管理
    Linux账户与权限管理
    MySQL实现读写分离
    SQL数据库常用函数
    MySQL进阶查询(二)
  • 原文地址:https://www.cnblogs.com/hbhzz/p/3198325.html
Copyright © 2011-2022 走看看