zoukankan      html  css  js  c++  java
  • 委托基应用实例

     一

    提问:能不能把方法作为参数传递?????

    也即使能能不能声明一个存放方法的变量呢——委托

    委托是一种数据类型,像类一样(可以生命委托类型变量),委托是用来接受方法的,通过委托可以吧方法作为参数进行传递

        class Program
        {
            public delegate void AddDelegate(); //定义无返回值无参数的委托
    
            static void Main(string[] args)
            {
                //【1】第一种方式
                //AddDelegate my = add;
                //  my();
                //【2】第二种方式
                AddDelegate my = new AddDelegate(add);
                my.Invoke();
                Console.ReadKey();
    
                //你定义的委托没有参数没有返回值,方法也是要没有参数没有返回值的
                //在使用委托的时候 通过委托变量来调用  可以把Invoke方法省略掉。
                
            }
    
            public static void add()
            {
    
                Console.WriteLine("恭喜你");
            }
        }

    二 

     需求:假设一件事情在前面和后面要做的事情比较固定,(这里假设输出“==========”),但是中间要做的事情经常发生改变,(比如

    1.要输出系统当前时间到控制台,2.要输出系统当前是星期几3.要吧系统时间写入到文本文件等)。

        class Class1
        {
            public void Print(PrintDelegate a)
            {
                Console.WriteLine("==============");
                Console.WriteLine("==============");
                //中间的事情经常变化的,需要用委托来传递过来一个方法
                //判断委托是否有方法传递过来
                if (a!=null)
                {
                    a.Invoke();
                }
             
                Console.WriteLine("==============");
                Console.WriteLine("==============");
            }
          在Programe中调用
         static void Main(string[] args) { Class1 my = new Class1(); //实例化这个类 my.Print(printday); //调用print方法,把你需要显示的内容的方法 传递过来 Console.ReadKey(); } //定义一个显示当前系统时间的方法 public static void printsystem() { Console.WriteLine(DateTime.Now); } //定义一个显示今天是星期几的方法 public static void printday() { Console.WriteLine(DateTime.Now.DayOfWeek); }

    从上例可以看出委托一般是在一个方法中间“挖个坑” 这个坑用来执行另一个方法,而这个方法是动态的,可以根据需要调用不同的方法到里面。

      需求:对字符串的处理经常需要发生变化,比如在字符串两端添加“=”或“+”号把字符串字符全部转化成大写。

        public delegate string PrintDelegate(string str);   //定义返回值string 参数string类型的委托
    
        class Class1
        {
            //定义方法返回值无,参数字符串数组,委托方法
            public void Getstring(string [] strs,PrintDelegate weit)
            {
                //循环遍历数组
                for (int i = 0; i < strs.Length; i++)
                {
                    //数组的每个下标=委托方法返回的值
                    strs[i] = weit(strs[i]);
                }
            }
    
        
        }
     class Program
        {
    
            static void Main(string[] args)
            {
                //定义字符串数组
                string[] strs =  {"zhangsan","lisi","wangwu" };
                //实例化
                Class1 my = new Class1();
                //调用方法
                 my.Getstring(strs, StrToupper);
                //循环输出结果
                for (int i = 0; i <strs.Length; i++)
                {
                    Console.WriteLine(strs[i]);
                }
             
    
                Console.ReadKey();
            }
    
            //定义委托方法返回string类型参数string  要和委托一致
            static string set(string a)
            {
                //把string字符串前后都加上“====”
                return "===="+a + "====";
    
            }
            //定义委托方法把字符串转换成大写
            static string StrToupper(string str)
            {
                return str.ToUpper();
            }
        }
  • 相关阅读:
    pycharm 使用pip3更新插件已经更新时报错
    剑指Offer系列之题11~题15
    剑指Offer系列之题1~题5
    个人hexo博客(静态,无后台)搭建
    设计模式之单例模式
    Hibernate实现limit语句效果
    Springboot项目中 前端展示本地图片
    eclipse报错:problems during content assist
    python中open与with open的区别
    修改Jenkins目录
  • 原文地址:https://www.cnblogs.com/xiaowie/p/9390174.html
Copyright © 2011-2022 走看看