1.委托(delegate)调用静态方法

委托类似于C++中的函数指针。

某方法仅仅在执行的时候才能确定是否被调用。

是实现事件和回调函数的基础。

面向对象,安全性高.

using System;
using System.IO; namespace IO
{
class Program
{
// 声明一个delegate(委托)
delegate int NumberChanger(int n);
static int num = ;
static void Main(string[] args)
{
// 实例化一个委托,构造函数内是符合要求的静态函数
NumberChanger nc1 = new NumberChanger(AddNum);
// 调用方式与调用方法一致
nc1();
Console.WriteLine(num);
} // 声明一个符合要求的静态方法,该方法的返回值以及参数列表必须与所声明的委托一致 public static int AddNum(int p)
{
num += p;
return num;
}
} }

2.通过委托调用实例化方法

using System;
using System.IO; namespace IO
{
class Program
{
// 声明一个delegate(委托)
delegate int NumberChanger(int n);
static void Main(string[] args)
{
MyClass mc = new MyClass();
NumberChanger nc2 = new NumberChanger(mc.AddNum);
Console.WriteLine(nc2());
} } class MyClass
{
private int num = ;
public int AddNum(int p)
{
num += p;
return num;
}
} }

3. multi-delegete(多重委托)

同时委托调用多个方法

using System;
using System.IO; namespace IO
{
class Program
{
delegate void D(int x);
static void Main(string[] args)
{ D cd1 = new D(C.M1);
cd1(-);
Console.WriteLine();
D cd2 = new D(C.M2);
cd1(-);
Console.WriteLine();
D cd3 = cd1 + cd2;
cd3();
Console.WriteLine(); C c = new C();
D cd4 = new D(c.M3);
cd3 += cd4;
cd3();
Console.WriteLine(); cd3 -= cd4;
cd3();
} } class C
{
public static void M1(int i)
{
Console.WriteLine("C.M1" + i);
} public static void M2(int i)
{
Console.WriteLine("C.M2" + i);
} public void M3(int i)
{
Console.WriteLine("C.M3" + i);
}
} }

MVC09的更多相关文章

  1. MVC-09安全

    部分8:添加安全. MVC应用程序安全性 Models文件夹包含表示应用程序模型的类. Visual Web Developer自动创建AccountModels.cs文件,该文件包含用于应用程序认证 ...

随机推荐

  1. Linux Shell命令总结

    关机/重启 关机(必须用root用户) shutdown -h now ## 立刻关机 shutdown -h + ## 10分钟以后关机 shutdown -h :: ##12点整的时候关机 hal ...

  2. [Algo] 26. Kth Smallest Number In Sorted Matrix

    Given a matrix of size N x M. For each row the elements are sorted in ascending order, and for each ...

  3. struts-dojo的使用

    1.导入struts2-dojo-plugin-2.1.8.jar 2.在用使用dojo的页面引入 <span style="font-size:14px;">< ...

  4. 动态添加checkbox

    <!--动态添加 checkbox--> <script type="text/javascript"> var data = new Array(); & ...

  5. [LC] 15. 3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  6. 2019ICPC南京网络赛B super_log(a的b塔次方)

    https://nanti.jisuanke.com/t/41299 分析:题目给出a,b,mod求满足条件的最小a,由题目的式子得,每次只要能递归下去,b就会+1,所以就可以认为b其实是次数,什么的 ...

  7. 从源码看commit和commitAllowingStateLoss方法区别

    Fragment介绍 在很久以前,也就是我刚开始写Android时(大约在2012年的冬天--),那时候如果要实现像下面微信一样的Tab切换页面,需要继承TabActivity,然后使用TabHost ...

  8. spring5.0.7.RELEASE配置jackson2.9.5

    概述 Jackson框架是基于Java平台的一套数据处理工具,被称为“最好的Java Json解析器”. 1.环境: jdk版本:jdk1.8spring版本:5.0.7.RELEASE jackso ...

  9. h-index|IF|Good story|IPS

    科研论文写作 科研论文写作的关键在于写出研究的重要性. 对科研工作者的评价标准主要以论文为主,可以从论文的定性和定量角度评价.论文的外部评价,包括科学院分区(包括123类):影响因子IF,可以通过we ...

  10. 【clientX,offsetX,screenX】 【scrollWidth,clientWidth,offsetWidth】的区别

    一.深刻认识clientX,offsetX,screenX 概念(来源于网络): clientX 设置或获取鼠标指针位置相对于当前窗口的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条. clie ...