好文档 - 专业文书写作范文服务资料分享网站

委托和事件(精选易懂实例代码)

天下 分享 时间: 加入收藏 我要投稿 点赞

using System;

/*要仔细研究,因为这是创新*/

/* 步骤1,声明delegate对象 */

public delegate void MyEventHandler(object sender, EventArgs e); class EventTest {

/* 2.实现方法,这是我们欲传递的方法,它与MyDelegate具有相同的参数和返回值类型 */ public static void MyEventFunc(object sender, EventArgs e) {

Console.WriteLine(\); }

/*3.通过委托定义事件*/

private event MyEventHandler myevent; /*4.订阅方法*/ public EventTest() {

this.myevent += new MyEventHandler(MyEventFunc); }

//5.触发事件

public void RaiseEvent() {

EventArgs e = new EventArgs(); if (myevent != null) {

myevent(this, e);

} }

public static void Main() {

EventTest et = new EventTest(); Console.Write(\); string s = Console.ReadLine(); if (s == \) {

et.RaiseEvent(); } else {

Console.WriteLine(\); }

Console.ReadKey(); } }

/*〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓*/

/* 步骤1,声明delegate对象 */

public delegate void MyEventHandler(object sender, EventArgs e); class EventTest {

/* 2.实现方法,这是我们欲传递的方法,它与MyDelegate具有相同的参数和返回值类型 */ public static void MyEventFunc(object sender, EventArgs e)

{

Console.WriteLine(\); }

/*3.通过委托定义事件*/

private event MyEventHandler myevent; /*4.订阅方法*/ public EventTest() {

myevent += MyEventFunc; }

//5.触发事件

public void RaiseEvent() {

EventArgs e = new EventArgs(); if (myevent != null) {

myevent(this, e);

} }

public static void Main() {

EventTest et = new EventTest(); et.RaiseEvent();

Console.ReadKey(); } }

/*〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓*/

/*委托:

委托是一个类,这个类保存了一个对象和这个对象的一个方法, 所以当你调用这个委托的时候就是调用保存的对象的那个方法,

委托的意义就是这个意思,把调用委托实际是委托给了一个对象的一个方法。 因为委托是一个类,所以他是定义在类级别上的,也就是和类同级别的。*/

/*1. 定义委托(方法签名和返回值确定)*/ public delegate void ComputeDelegate(int a, int b); public class program {

/*2.实现方法*/

void Add(int a, int b) /*非静态方法必须先把类实例化再被调用*/ {

Console.WriteLine(a + b); }

static void Sub(int a, int b)/*静态方法无需把类实例化,可以直接被调用*/ {

Console.WriteLine(a - b); }

static void Main(string[] args) {

program p = new program();/*把类实例化,以便用类名p来调用类中的方法*/

/*3.实例化委托*/

ComputeDelegate compute = new ComputeDelegate(p.Add); compute += Sub; /*多播委托一次调用多个方法形成队列*/ /*4.通过委托用方法*/ compute(300, 100); Console.ReadKey(); } }

/*〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓*/

/*事件:

事件是一种类的成员,他同方法,字段,属性是一个级别的, 所以它定义在类的内部,你可以很容易区分开.

和字段,属性一样,事件也有事件类型,比如一个叫Name的属性是String类型的, 那么一个叫MouseMove的事件他是一个叫EventHandler的委托类型的 事件是多点委托,对事件只能使用 += 和 -= 运算*/

/*1.定义特殊委托(签名:事件发出者,事件参数,无返回值)*/ public delegate void SampleDelegate(object sender, EventArgs e); public class Program {

/*2.实现方法*/

void Program_SampleEvent(object sender, EventArgs e) {

Console.WriteLine(\执行事件1\); }

void Program_ShowEvent(object sender, EventArgs e) {

Console.WriteLine(\执行事件2\); }

/*3.通过委托定义事件*/

public event SampleDelegate SampleEvent; /*4.订阅方法*/

public void MyEvent() {

SampleEvent += Program_SampleEvent; SampleEvent += Program_ShowEvent; }

/*5.触发事件*/

public void RaiseEvent() {

EventArgs e = new EventArgs(); if (SampleEvent != null) {

SampleEvent(this, e); } }

static void Main(string[] args) {

Program p = new Program(); p.MyEvent(); p.RaiseEvent(); Console.ReadKey(); }

}

/*〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓*/

/*我们来模拟一个打字智能机,他有三部分构成:在键盘上面输入文字、输出在显示器、 语音提示;如果要实现这三种操作必须需要三种不同的硬件,所以*/ /*键盘只能实现打字、显示器实现输出,语音提示设备实现读出文字。 所以我们应该让他们看成三种不同的对象,来实现程序!*/ /*定义三个类,Smart(智能机类),Typing(打字方法), show(显示方法),MakyVoice(语音提示方法)*/ /*键盘打字*/

class Program {

public class Smart {

public delegate void SmartDelegate(char T); /*定义一个委托 */ public event SmartDelegate SmarEvent;/*定义实现这个委托的事件*/ public char T;/*相当于你每一次打的单个文字*/

/*定义一个字符串相当于我们从键盘上打出来的文字…… 呵 */

public string Text = \解放四大快捷方式打开附件多撒即可了飞洒富商大贾快乐看附件撒疯狂\; public void Typing() {

foreach (char t in Text) {

T = t;

if (SmarEvent != null) {

SmarEvent(T); } } } }

/*显示输出*/

public class Display {

public void show(char T) {

Console.WriteLine(T); } }

/*语音提示*/ public class Voice {

public void MakyVoice(char T) {

Console.WriteLine(\您输出了:\ + T); } }

static void Main(string[] args) {

Smart S = new Smart(); Display D = new Display(); Voice V = new Voice();

S.SmarEvent += new Smart.SmartDelegate(D.show);

S.SmarEvent += new Smart.SmartDelegate(V.MakyVoice);

S.Typing();

Console.ReadKey(); } }

/*〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓*/

public delegate void SmartDelegate(char T); /*定义一个委托 */ public class Smart {

public char T;

public string Text = \解放四大快捷方式打开附件多撒即可了飞洒富商大贾快乐看附件撒疯狂\; public void show(char T) {

Console.WriteLine(T); }

public void MakyVoice(char T) {

Console.WriteLine(\您输出了:\ + T); }

public event SmartDelegate SmarEvent;/*定义实现这个委托的事件*/

void MyEvent() {

SmarEvent += show;

SmarEvent += MakyVoice; }

public void Typing() {

foreach (char t in Text) {

T = t;

if (SmarEvent != null) {

SmarEvent(T); } } }

static void Main(string[] args) {

Smart S = new Smart(); S.MyEvent(); S.Typing();

Console.ReadKey(); } }

/*〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓*/

/*定义委托,它定义了可以代表的方法的类型*/ public delegate void GreetingDelegate(string name); class Program {

private static void EnglishGreeting(string name) {

Console.WriteLine(\ + name); }

委托和事件(精选易懂实例代码)

usingSystem;/*要仔细研究,因为这是创新*//*步骤1,声明delegate对象*/publicdelegatevoidMyEventHandler(objectsender,EventArgse);classEventTest{/*2.实现方法,这是我们欲传递的
推荐度:
点击下载文档文档为doc格式
9fo3c83iaq7wp9920syv
领取福利

微信扫码领取福利

微信扫码分享