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+tomcat配置集群

    安装nginx以及两个以上tomcat,并启动 配置集群nginx/conf/nginx.conf文件 说明:server_list为名字,可以在每台服务器ip后面添加weight number,设置 ...

  2. js 实现时间递增,当前时间功能,javascript格式化当天日期

    直接上代码: function nowTimeStr() {                        var date = new Date();                        ...

  3. Jmeter连接Mysql出现Cannot create PoolableConnectionFactory (Could not create connection to database server.)错误

    0 环境 系统环境:win10 1 正文 一般是数据库的驱动包版本不匹配(我是直接放在jmeter/lib下的) 当然有时候需要添加?useUnicode=true&characterEnco ...

  4. sklearn包源码分析(二)——ensemble(未完成)

    网络资源 sklearn包tree模型importance解析

  5. day11-random模块-随机

    import random # 一.随机小数: print(random.random()) # 0.848972270116501结果是0-1之间的随机小数 print(random.uniform ...

  6. 学习python-20191108(2)REST接口相关

    一.客户登录验证 在使用接口前,需要对客户进行登录验证 enums.py文件代码: #定义枚举,客户端登录的方式有很多种形式:邮箱登录.手机登录.微信小程序登录.微信公众号登录 class Clien ...

  7. python后端面试第三部分:数据储存与缓存相关--长期维护

    1. 列举常见的关系型数据库和非关系型都有哪些?2. MySQL常见数据库引擎及比较?3. 简述数据三大范式?4. 什么是事务?MySQL如何支持事务?5. 简述数据库设计中一对多和多对多的应用场景? ...

  8. Null Hypotheses| Alternative Hypotheses|Hypothesis Test|Significance Level|two tailed |one tailed|

    9.1 The Nature of Hypothesis Testing Over the years, however, null hypothesis has come to mean simpl ...

  9. python使用geopandas和shapely处理shp文件

    一.环境搭建 所需库:geopandas (以及前置库)  doc:http://geopandas.org/ shapely(以及前置库)  doc: 二.数据预处理 1.将shp文件进行切片 2. ...

  10. better-scroll插件的介绍及使用

    在我们日常的移动端项目开发中,处理滚动列表是再常见不过的需求了,可以是竖向滚动的列表,也可以是横向的,用better-scroll可以帮助我们实现这个 什么是 better-scroll better ...