• c# 委托实例


    
    
     class Program
        {
            static void HandleDemoEvent(object sender, EventArgs e)
            {
                Console.WriteLine("Handled by HandleDemoEvent");
            }
            static void Main(string[] args)
            {
                Console.WriteLine("c#1 委托示例");
                Person jon = new Person("Jon");
                Person tom = new Person("Tom");
                StringProcessor josVoice, tomsVoice, background;
                //创建3个委托实例
                josVoice = new StringProcessor(jon.Say);
                tomsVoice = new StringProcessor(tom.Say);
                background = new StringProcessor(Background.Note);
    
                josVoice("Hello,Lichuan");
                tomsVoice.Invoke("Hello,Doddy!");
                background("An airplane flies past.");
    
    
    
                Console.WriteLine("c#2 委托示例");
                EventHandler handler;
                handler = new EventHandler(HandleDemoEvent);//指定委托类型和方法
                handler(null, EventArgs.Empty);
    
                handler = HandleDemoEvent;//隐式转换成委托实例
                handler(null, EventArgs.Empty);
    
                handler = delegate(object sender, EventArgs e)//用一个匿名方法来指定操作
                {
                    Console.WriteLine("Handled anonymously");
                };
                handler(null, EventArgs.Empty);
    
                handler = delegate         //使用匿名方法的简写
                {
                    Console.WriteLine("Handled anonymously again");
                };
                handler(null, EventArgs.Empty);
    
    
    
    
    
                Console.ReadKey();
            }
        }
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SRLJCSHAP.委托.Demo
    {
        delegate void StringProcessor(string input);//声明委托类型
        public class Person
        {
            string name;
            public Person(string Name)
            {
                this.name = Name;
            }
            //声明兼容的实例方法
            public void Say(string Msg)
            {
                Console.WriteLine("{0} says: {1}", name, Msg);
            }
    
    
        }
        public class Background
        {
            //声明兼容的静态方法
            public static void Note(string note)
            {
                Console.WriteLine("({0})", note);
            }
    
           
        }
    }
  • 相关阅读:
    global s power in php...
    null is empty
    如何创建spring web 工程
    如何下载spring sts
    使用apache-commons-lang3架构对HTML内容进行编码和反编码
    SQL 查询建表SQL
    kindeditor 在JSP 中上传文件的配置
    在java web工程中jsp页面中使用kindeditor
    实现<base>标签中有绝对路径
    实现多个JSP页面共用一个菜单
  • 原文地址:https://www.cnblogs.com/liuruitao/p/4289790.html
Copyright © 2020-2023  润新知