• C#委托与事件


    用委托实现同时关闭电视,电脑,电灯。

    一 委托:

     //定义一个电视机类
        public class TV
        { 
           //定义一个关闭电视机的方法
            public void shutTV()
            {
                Console.WriteLine("关闭电视机");
            }
    
        }
        //定义一个电脑类
        public class Computer
        { 
            //关闭电脑
            public void shutComputer()
            { 
               Console.WriteLine("关闭电脑");
            }
        }
        //定义一个电灯类
        public class Light
        { 
           //关闭电灯
            public void shutLight()
            {
                Console.WriteLine("关闭电灯");
            }
        }

    //定义一个控制类

     public class Controller
        {
            //定义字段
            private TV tv;
            private Computer computer;
            private Light light;
            //属性get,set
            public TV TV {get;set;}
            public Computer Computer { get; set; }
            public Light Light { get; set; }
            //构造函数
            public Controller(){}
    
            //定义委托类型stopMachine ,返回值为Void, stopMachine()的参数列表为空。
            //所以要求委托指向的方法返回值也为空,参数列表也为空。
            //delegate关键字相当于类类型的class关键字
            public delegate void stopMachine();
            //声明一个stopmMachine类型的对象
            public stopMachine stopmachine;
            //添加委托
            public void Add(stopMachine stopmethod)
            {   //实例化对象
                this.stopmachine += stopmethod;
                //相当于
                //stopmachine = new stopMachine(tv.shutTV);
                //stopmachine = new stopMachine(computer.shutComputer);
                //stopmachine = new stopMachine(light.shutLight);
            }
            //删除委托
            public void ReMove(stopMachine stopmethod)
            {
                this.stopmachine -= stopmethod;
            }
            //调用委托
            public void shutDown()
            {
                stopmachine();
            }
        }

    //在main函数中调用

    public class Program
        {
            private static void Main(string[] args)
            {
    
                //实例化类
                Controller controller = new Controller();
                TV tv = new TV();
                Computer computer = new Computer();
                Light light = new Light();
                //添加委托
                controller.Add(tv.shutTV);
                controller.Add(computer.shutComputer);
                controller.Add(light.shutLight);
                //删除委托
                controller.ReMove(tv.shutTV);
                //调用委托
                controller.shutDown();
                Console.ReadKey(); 
            }
        }

    //效果图:

     因为main函数中删除了对关闭电视的委托,所以运行结果只显示了关闭电脑和电灯。

    二 委托与Lambda表达式

       如果我的委托类型返回值是void 参数列表为空,但是我想调用的方法是返回值是string 参数列表不为空。

      如:

       public delegate void stopMachine(); //委托

       要调用如下方法:

       public void shutLight(int a){......}.//有参数

       第一适配器的方式  让委托指向closeLight方法。

       public void closeLight(){shutLight();};

       stopmachine +=light.closeLight;

       第二种使用lambda表达式:

       stopmachine +=(()=>{light.shutLight(3);});此时的lambda表达式相当于无参的匿名函数。lamdba的用法参考如下:

    Lambda表达式的示例:

       1,   x=x*x

    public class Program
        {

            delegate int TestLambda(int a);
            private static void Main(string[] args)
            {

                           TestLambda t1;

                           t1=x=>x*X;

                           int result=t1(10);

                           Console.WriteLine(result);

                           Console.ReadKey();
            }
        }

       运行结果:

    2, x=>{return x*x;}

    3,(int x)=>x/2

    4,()=>light.shutLight(3);

    5.(x,y)=>{x++;x/y;}

    三 委托与事件

     //定义一个电视机类
        public class TV
        { 
           //定义一个关闭电视机的方法
            public void shutTV()
            {
                Console.WriteLine("关闭电视机");
            }
    
        }
        //定义一个电脑类
        public class Computer
        { 
            //关闭电脑
            public void shutComputer()
            { 
               Console.WriteLine("关闭电脑");
            }
        }
        //定义一个电灯类
        public class Light
        { 
           //关闭电灯
            public void shutLight()
            {
                Console.WriteLine("关闭电灯");
            }
        }

    定义控制类:
        

     public class Controller
        {
            //定义字段
            private TV tv;
            private Computer computer;
            private Light light;
            //属性get,set
            public TV TV { get; set; }
            public Computer Computer { get; set; }
            public Light Light { get; set; }
            //构造函数
            public Controller() { }
    
            //定义委托类型stopMachine ,返回值为Void, stopMachine()的参数列表为空。
            //所以要求委托指向的方法返回值也为空,参数列表也为空。
            //delegate关键字相当于类类型的class关键字
            public delegate void stopMachine();
            //声明事件,事件的类型名称是委托的类型名称 如:stopMachine
            //事件在委托的基础上做了封装,事件的调用只能在类的里面
            public event stopMachine stopMachineEvent;
    
            public void shutDownEvent()
            {
                if (stopMachineEvent != null)
                {
                    this.stopMachineEvent();
                }
            }
        }

    在main函数中调用

    public class Program
        {
            private static void Main(string[] args)
            {
                //实例化类
                Controller controller = new Controller();
                TV tv = new TV();
                Computer computer = new Computer();
                Light light = new Light();
                //事件实例化
                controller.stopMachineEvent += tv.shutTV;
                controller.stopMachineEvent += computer.shutComputer;
                controller.stopMachineEvent += light.shutLight;
                //调用shutDownEvent方法 让类自己调用stopMachineEvent事件
                controller.shutDownEvent();
                Console.ReadKey();
            }
        }

    运行结果图:

    事件的调用只能是在定义事件类的内部,委托打破了封装。

      

      

  • 相关阅读:
    从尾到头打印链表
    剑指offer
    Codeforces Round #345
    算法入门系列之字符串
    【codenet】代码相似度计算框架调研 -- 把内容与形式分开
    【学习笔记--数据结构】合法的出栈序列与栈混洗
    我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
    【PAT L2-001】最短路计数
    【CF689D Friends and Subsequences】二分搜索,区间查询
    【编译原理】语法分析LL(1)分析法的FIRST和FOLLOW集
  • 原文地址:https://www.cnblogs.com/zcttxs/p/2560154.html
Copyright © 2020-2023  润新知