zoukankan
html css js c++ java
一个C#实现的最简单的委托例子
using
System;
namespace
DelegateSample
{
public
delegate
void
PrintCallback(
int
number);
public
class
Printer
{
//
委托定义
private
PrintCallback _print;
//
委托将要依附的属性
public
PrintCallback PrintCallback
{
get
{
return
_print;}
set
{_print
=
value;}
}
}
public
class
Driver
{
//
将要委托的事件
private
void
PrintInteger(
int
number)
{
Console.WriteLine(
"
From PrintInteger:The number is {0}.
"
, number);
}
static
void
Main(
string
[] args)
{
Driver driver
=
new
Driver();
Printer printer
=
new
Printer();
//
将委托绑定到属性
printer.PrintCallback
=
new
PrintCallback(driver.PrintInteger);
//
使用属性触发委托事件
printer.PrintCallback(
10
);
printer.PrintCallback(
100
);
Console.WriteLine(
"
press Enter to exit
"
);
Console.ReadLine();
}
}
}
结果:
From PrintInteger:The number
is
10
.
From PrintInteger:The number
is
100
.
press Enter to exit
我来自:
向东博客
查看全文
相关阅读:
2019春季助教学期总结
第二次实验设计报告
第十二周作业
第十一周作业
第十周作业
第九周作业
第八周作业
万恶的第七周作业
第六周作业
堆积如山的第五周作业
原文地址:https://www.cnblogs.com/meil/p/435660.html
最新文章
又是线段树(维护方差)
第01组 Alpha冲刺(4/6)
第01组 Alpha冲刺(3/6)
第01组 Alpha冲刺(2/6)
第01组 Alpha冲刺(1/6)
第01组 团队Git现场编程实战
第01组 团队项目-需求分析报告
团队项目-选题报告
第二次结对编程作业
第1组团队展示
热门文章
第一次结对编程作业
7-2 冒泡法排序 (20分)
7-1 最大子列和问题 (20分)
约瑟夫环(数组、链表)
模拟测试
约瑟夫环问题
1003 我要通过! (20 分)
听周筠老师的“私人讲座”
线性表
数据结构和算法绪论
Copyright © 2011-2022 走看看