zoukankan      html  css  js  c++  java
  • C# 关于委托的小例子

    本例子是一个关于委托的小例子[猫叫,狗跳,人喊]。仅供学习分享使用,如有不足之处,还请指正。

    概述

    委托是C#开发中一个非常重要的概念,理解起来也和常规的方法不同,但一旦理解清楚,就可以信手拈来,随处可用。

    委托是对方法的抽象。它存储的就是一系列具有相同签名和返回回类型的方法的地址。调用委托的时候,委托包含的所有方法将被执行。

    涉及知识点

    委托的定义

    • 以deleagate关键字开头。
    • 返回类型+委托类型名+参数列表。

    效果图

    如下【点击打开猫,弹出CatForm,点击喵喵按钮,触发事件,调用其他类的委托】:

    核心代码

    代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace DemoDelegate
     7 {
     8     public delegate void Handler();//定义一个委托类型
     9     /// <summary>
    10     /// 猫猫
    11     /// </summary>
    12     public class Cat
    13     {
    14 
    15         public Handler Miao;
    16 
    17         /// <summary>
    18         /// Miao叫的动作
    19         /// </summary>
    20         public void MaioMiao()
    21         {
    22             var action = Miao;
    23             if (action != null)
    24             {
    25                 action();
    26             }
    27         }
    28     }
    29 }
    30 ///////////////////////////////////////////////////
    31 using System;
    32 using System.Collections.Generic;
    33 using System.Linq;
    34 using System.Text;
    35 
    36 namespace DemoDelegate
    37 {
    38     /// <summary>
    39     /// 可爱的狗狗
    40     /// </summary>
    41     public class Dog
    42     {
    43         public Handler Tiao;
    44 
    45         /// <summary>
    46         /// 狗跳的动作
    47         /// </summary>
    48         public void DogTiao()
    49         {
    50             var action = Tiao;
    51             if (action != null)
    52             {
    53                 action();
    54             }
    55         }
    56     }
    57 }
    58 
    59 ///////////////////////////////////////////////
    60 
    61 using System;
    62 using System.Collections.Generic;
    63 using System.Linq;
    64 using System.Text;
    65 
    66 namespace DemoDelegate
    67 {
    68     /// <summary>
    69     /// 主人
    70     /// </summary>
    71     public class Master
    72     {
    73         public Handler Han;
    74 
    75         public void HanJiao()
    76         {
    77             var action = Han;
    78             if (action != null)
    79             {
    80                 action();
    81             }
    82         }
    83 
    84     }
    85 }
    View Code

    界面类代码如下:

      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 
     10 namespace DemoDelegate
     11 {
     12     public partial class MainForm : Form
     13     {
     14         private Dog dog;
     15 
     16         private Master master;
     17 
     18         public MainForm()
     19         {
     20             InitializeComponent();
     21             InitInfo();
     22         }
     23 
     24         /// <summary>
     25         /// 初始化信息
     26         /// </summary>
     27         private void InitInfo() {
     28             dog = new Dog();
     29             dog.Tiao += TiaoMethod;
     30             master = new Master();
     31             master.Han += HanMethod;
     32         }
     33 
     34         /// <summary>
     35         /// 喵喵
     36         /// </summary>
     37         /// <param name="sender"></param>
     38         /// <param name="e"></param>
     39         private void btnCatMiao_Click(object sender, EventArgs e)
     40         {
     41             CatForm catForm = new CatForm();
     42             catForm.MiaoAction += MaioMethod;
     43             catForm.ShowDialog();
     44         }
     45 
     46         private void MaioMethod(object sender,EventArgs e) {
     47             this.txtCat.Text = "猫在父页面叫了";
     48             this.dog.DogTiao();
     49             this.master.HanJiao();
     50         }
     51 
     52         private void TiaoMethod()
     53         {
     54             this.txtDog.Text = "狗跳了";
     55         }
     56 
     57         private void HanMethod()
     58         {
     59             this.txtMaster.Text = "别叫了";
     60         }
     61     }
     62 }
     63 /////////////////////////////////////////////////////////////////
     64 using System;
     65 using System.Collections.Generic;
     66 using System.ComponentModel;
     67 using System.Data;
     68 using System.Drawing;
     69 using System.Linq;
     70 using System.Text;
     71 using System.Windows.Forms;
     72 
     73 namespace DemoDelegate
     74 {
     75     public partial class CatForm : Form
     76     {
     77         private Cat cat;
     78 
     79         public event EventHandler MiaoAction;
     80 
     81         public CatForm()
     82         {
     83             InitializeComponent();
     84             InitInfo();
     85         }
     86 
     87         public void InitInfo() {
     88             cat = new Cat();
     89             cat.Miao += MaioMethod;
     90         }
     91 
     92         private void MaioMethod()
     93         {
     94             this.txtCat.Text = "猫叫了";
     95             var action = MiaoAction;
     96             if (action != null) {
     97                 action(cat, null);
     98             }
     99         }
    100 
    101         private void btnCatMiao_Click(object sender, EventArgs e)
    102         {
    103             this.cat.MaioMiao();
    104         }
    105     }
    106 }
    View Code


    关于委托,功能和用途还有很多,这里只是抛砖引玉,希望能对大家有帮助

     源码下载

  • 相关阅读:
    C# 技能鉴定 第三单元 第四单元题目总结
    C# 技能鉴定 第三单元 test 3_5
    C# 技能鉴定 第三单元 Test3_4
    C# 技能鉴定 第三单元的题目
    班级通讯录修改与维护
    C# 技能鉴定
    Windows 编程入门,如何注册账号
    Windows 编程入门,了解开发UWP应用的准备工作
    logback-spring.xml
    springboot和mybatis 配置多数据源
  • 原文地址:https://www.cnblogs.com/hsiang/p/6275771.html
Copyright © 2011-2022 走看看