惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
Google DeepMind News
Google DeepMind News
U
Unit 42
博客园 - 叶小钗
博客园 - 聂微东
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
aimingoo的专栏
aimingoo的专栏
D
DataBreaches.Net
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
美团技术团队
The Cloudflare Blog
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
S
Schneier on Security
C
Check Point Blog
Project Zero
Project Zero
The Hacker News
The Hacker News
Scott Helme
Scott Helme
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cisco Talos Blog
Cisco Talos Blog
P
Privacy International News Feed
SecWiki News
SecWiki News
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
H
Help Net Security
TaoSecurity Blog
TaoSecurity Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Last Week in AI
Last Week in AI
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
T
Troy Hunt's Blog
H
Hacker News: Front Page
Vercel News
Vercel News

博客园 - 洪虎

Windows环境下安装Python 建立批命令快速修改本机IP地址 网络经典命令 - 洪虎 - 博客园 “指定域的名称或安全标识SID与该域的信任信息不一致”的解决方法 建立主DNS区域和辅助DNS区域的最佳实践 无人职守安装 XP 电脑黑客基本技术和入侵的关键字以及电脑攻防应用 建立一个可修改Windows域用户密码的web页面 修复SQL2000中损坏的表 将Excel转换为CSV的VBA代码 - 洪虎 - 博客园 用命令检查电脑是否被安装木马 DateTime.ToString 方法 - 洪虎 - 博客园 在C#中自定义异常 - 洪虎 - 博客园 使用DataDynamics.ActiveReports 创建子报表 错误的SQL脚本,错误消息 4104 - 洪虎 - 博客园 适配器模式 如何在 Reporting Services 开发基于用户权限的报表呢? 接口模型 使用反射检查程序集,实现自动更新
委托模式
洪虎 · 2006-10-11 · via 博客园 - 洪虎

与其他面向对象编程语言如C++不同,C#提供了另外一种回调方式,就是委托。我理解委托就是对同一类方法的声明。声明一个委托后,就好比定义了一类具有相同输入参数和返回参数的方法。而不必理会这些方法的名字是什么,由哪些类来实现,也不用理这些方法到底是做什么用的。有可能这些方法所实现的功能大相径庭。委托只关心这些方法的输入参数和返回参数。如果没有返回参数的委托可以看作是“多播委托”。

初看委托,觉得它有些像接口。但细细体会却觉得还是有差别。差别就在于委托关心的一类方法,类似C++中的函数指针。而接口关心的具有相同数据或方法的类。

 1using System;
 2using System.Collections;
 3
 4namespace Exam
 5{
 6    // 这里定义一个委托,用以表示所有对整形数字A和B进行处理后返回整形数据的方法
 7    public delegate int CountMethodDelegate(int a, int b);
 8
 9    class Class1
10    {
11        // 下面定义一些对A和B进行处理的方法
12
13        // 加法
14        static int Plus(int a, int b)
15        {
16            Console.WriteLine(a.ToString() + "+" + b.ToString() +" = ");
17            return a + b;
18        }

19
20        // 减法
21        static int Minus(int a, int b)
22        {
23            Console.WriteLine(a.ToString() + "-" + b.ToString() + " = ");
24            return a - b;
25        }

26
27        // 乘法
28        static int Multiply(int a, int b)
29        {
30            Console.WriteLine(a.ToString() + "*" + b.ToString() + " = ");
31            return a * b;
32        }

33
34        // 除法
35        static int Divide(int a, int b)
36        {
37            Console.WriteLine(a.ToString() + "/" + b.ToString() + " = ");
38            if (b != 0)
39                return a / b;
40            else
41                return 0;
42        }

43
44        [STAThread]
45        static void Main(string[] args)
46        {
47            int A, B;
48            A = 100;
49            B = 7;
50
51            // 连续向这个委托的实例添加方法。
52            CountMethodDelegate test = new CountMethodDelegate(Plus);
53            test += new CountMethodDelegate(Minus);
54            test += new CountMethodDelegate(Multiply);
55            test += new CountMethodDelegate(Divide);
56
57
58            // 调用委托test所代表的方法,同时传递他需要的参数A和B。
59            // 一次调用,执行所有方法。
60            Console.Write(test(A, B));
61
62            Console.ReadKey();
63        }

64    }

65}

66

可以看到输出结果,并不是我所期望的每个公式后的有结果出现。只有最后一个除法有结果。那这说明什么问题呢。就是在多播委托时,只有一个结果被返回。但为什么不一定是最后一个方法调用呢。因为书上有说dotNet框架只保证会执行全部委托的方法,但不确保执行的顺序是按照加入的顺序。这里测试几次都是最后一个结果被返回。有可能是因为我测试的量不够大。所以看不到其他的情况出现。

洪虎

2006-10-10