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. Nginx配置使用

    1.黑色标注的得自己写入到nginx.conf文件中 upstream serverlb { server 127.0.0.1:9999; server 127.0.0.1:8888; } serve ...

  2. Miller-Rabin素数检测算法

    遇到了一个题: Description: Goldbach's conjecture is one of the oldest and best-known unsolved problems in ...

  3. spring-mvc基于注解的配置

    将配置文件修改为: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  4. <USACO09DEC>视频游戏的麻烦Video Game Troublesの思路

    emm今天模拟赛的题.神奇地A了 #include<cstdio> #include<cstring> #include<iostream> #include< ...

  5. Win7如何查看nvidia显卡(GPU)的利用率

    1.在文件夹C:\Program Files\NVIDIA Corporation\NVSMI里找到文件nvidia-smi.exe2.把该文件拖到命令提示符窗口(win+R,再输入‘CMD’进入), ...

  6. axious设置携带cookie同时允许跨域的问题

    axious设置携带cookie同时允许跨域的问题

  7. vue使用日记

    使用vue-cli生成单页应用框架, npm run serve之后即可开始修改src目录下面的js css文件(index.html可以保持不动), 自动热部署 , 前端框架就已经搭起来了. web ...

  8. [LC] 62. Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. WEB前端资源集(二)

    在上一篇为大家整理出了一些资源网站,接下来给大家整理了一些开发中常用的工具. 开发工具篇 开发工具集 Sublime Text 3:SublimeText 3是一个代码编辑器,也是HTML和散文先进的 ...

  10. 2015-09-14-C++基础

    声明与定义 声音与定义的区别在于,声明没有给变量分配空间,而定义则给变量分配了空间:定义也是声明. extern int i; // 声明但未定义 int i ; //声明且定义 extern dou ...