A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 非常小可 中级黑马   /  2012-4-18 10:35  /  1491 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

谈谈委托的实现。什么是委托,事件是不是委托

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

4 个回复

倒序浏览
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Text.RegularExpressions;


  10. namespace heima
  11. {
  12.     public partial class Form2 : Form
  13.     {
  14.         public Form2()
  15.         {
  16.             InitializeComponent();
  17.         }

  18.         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  19.         {
  20.             if (textBox1.Text.Length >= 2)
  21.             {
  22.                 string s =textBox1.Text.Substring(0, 1) ;
  23.                 Console.Write(textBox1.Text.Substring(1, 1));

  24.                 if (textBox1.Text.Substring(0, 1) == "0" && textBox1.Text.Substring(1, 1) == "0")
  25.                 {
  26.                   
  27.                     textBox1.Text = "";
  28.                   
  29.                 }
  30.             }

  31.            
  32.         }

  33.         private void button1_Click(object sender, EventArgs e)
  34.         {
  35.             decimal  i = decimal.Parse(textBox1.Text);
  36.             int j = int.Parse(textBox2.Text);
  37.             switch (comboBox1.SelectedIndex)
  38.             {
  39.                 case 0:
  40.                     label5.Text = "加法运算结果:" + (i + j);
  41.                     break;
  42.                 case 1:
  43.                     label5.Text = "加法运算结果:" + (i - j);
  44.                     break;

  45.                 case 2:
  46.                     label5.Text = "加法运算结果:" + (i * j);
  47.                     break;

  48.                 case 3:
  49.                     label5.Text = "加法运算结果:" + (i / j);
  50.                     break;


  51.             
  52.             }

  53.         }

  54.         private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
  55.         {
  56.             if (textBox2.Text.Length >= 2)
  57.             {
  58.                 string s = textBox2.Text.Substring(0, 1);
  59.                 Console.Write(textBox2.Text.Substring(1, 1));

  60.                 if (textBox2.Text.Substring(0, 1) == "0" && textBox2.Text.Substring(1, 1) == "0")
  61.                 {

  62.                     textBox2.Text = "";

  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

回复 使用道具 举报
委托是一种安全的函数指针,事件是一种消息机制
委托可以把一个方法作为参数代入另一个方法。

事件的内部是用委托实现的。
事件是一种特殊的委托,一般在一个类里面定义一个委托,那么我们可以通过类“实例.委托名”来调用这个委托

代码里委托你不但可以安排谁是它的调用函数,还可以直接调用它,而事件不能直接调用,只能通过某些操作触发

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

回复 使用道具 举报
第一步:
    class Program
    {
        static void englishhello(string name)
        {
            Console.WriteLine("hello:" + name);
        }
        static void chinesehello(string name)
        {
            Console.WriteLine("你好:" + name);
        }
        static void Main(string[] args)
        {
            englishhello("jodon");
            chinesehello("张三");
        }
    }
    第二步:
    class Program
    {
        static void englishhello(string name)
        {
            Console.WriteLine("hello:" + name);
        }
        static void chinesehello(string name)
        {
            Console.WriteLine("你好:" + name);
        }
        static void telegatePerson(string name,string language)
        {
            switch (language)
            {
                case "English":
                    englishhello(name);
                    break;
                case "Chinese":
                    chinesehello(name);
                    break;
                default:
                    Console.WriteLine("火星来的吧");
                    break;
            }
        }
        static void Main(string[] args)
        {
            telegatePerson("jodon","English");
            telegatePerson("张三","Chinese");
        }
    }
    第三步:
    public delegate void mydelegate1();
    public delegate void mydelegate2(string name);
    class Program
    {
        static void test()
        {
            Console.WriteLine("test");
        }
        static void englishhello(string name)
        {
            Console.WriteLine("hello:" + name);
        }
        static void chinesehello(string name)
        {
            Console.WriteLine("你好:" + name);
        }
        static void telegatePerson(string name,mydelegate2 language,mydelegate1 t)
        {
            language(name);
            t();
        }
        static void Main(string[] args)
        {
            telegatePerson("jodon",englishhello,test);
            telegatePerson("张三",chinesehello,test);
        }
    }
    第四步:
    public delegate void mydelegate1();
    public delegate void mydelegate2(string name);
    class Person
    {
        public static void test1()
        {
            Console.WriteLine("static");
        }
        public void test2()
        {
            Console.WriteLine("auto");
        }
    }
    class Program
    {        
        static void Main(string[] args)
        {
            mydelegate1 d1, d2;
            d1 = Person.test1;

            Person p=new Person();
            d2 = p.test2;

            d1();
            d2();
        }
    }
    第五步:
     public delegate void mydelegate1();
    public delegate void mydelegate2(string name);
    class Person
    {
        public static void test1()
        {
            Console.WriteLine("static");
        }
        public  void test2()
        {
            Console.WriteLine("auto");
        }
    }
    class Program
    {        
        static void Main(string[] args)
        {
            mydelegate1 d;
            d = Person.test1;

            Person p = new Person();
            d+=p.test2;

            d();
        }
    }

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

回复 使用道具 举报
委托就是拿函数当参数来用
用来传递方法

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马