用 事件 时,必须 要委托么?
搜了委托的示例,也会提到事件,但是演示 委托功能时,可以不用事件
但是,搜索所有事件的例子,基本都会有委托
用事件时,一定要委托么 ?
再有,为什么要使用事件? 什么情况下适合使用事件?
[解决办法]
事件就是一种委托
[解决办法]
Sender sender = new Senser();
Receiver receiver = new Receiver(sender);
sender.TriggerEvent();
using System.Text;
namespace T2
{
//定义一个业务逻辑类
class HelloManager
{
//把委托作为方法的参数
public void SayHello(string name, HelloHander hander) {
hander(name);
}
}
//定义中国人类
class Chinaes
{
public static void ChinesHello(string name) {
Console.WriteLine("ChinesHello," + name);
}
}
//定义英国人类
class English
{
public static void EnglishHello(string name)
{
Console.WriteLine("EnglishHello," + name);
}
}
//定义委托
public delegate void HelloHander(string name);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace T2
{
class Program
{
static void Main(string[] args)
{
//调用HelloManager的例子
HelloManager h = new HelloManager();
h.SayHello("哈哈",English.EnglishHello);
}
}
}
------------------------------------------------------------------自定义事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace T2
{ //自定义注册事件
//猫是事件原,所以事件类型要在猫类里面定义
class Cat
{
//定义委托
public delegate void CryHander();
//定义事件,委托类型
public event CryHander CryEvent;
public void Cry() {
Console.WriteLine("猫大叫一声...");
if (CryEvent != null) {
CryEvent();//执行事件
}
}
}
//老鼠
class Mouse {
public void Run() {
Console.WriteLine("老鼠逃跑了....");
}
}
//主人
class Master {
public void Weak() {
Console.WriteLine("主人醒了....");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace T2
{
class Program
{
static void Main(string[] args)
{
//动态注册事件
Cat cat = new Cat();
Mouse mouse = new Mouse();
Master master = new Master();
//订阅事件,多播性
cat.CryEvent += new Cat.CryHander(mouse.Run);
cat.CryEvent += new Cat.CryHander(master.Weak);
//触发事件
cat.Cry();
}
}
}
--------------------------------------带参数的事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace T2
{
//事件类型
public class QuestionEvenArgs : EventArgs
{
public string QuestionMes{get; set; }
}
//委托
public delegate void QuestionHander(object sendr,QuestionEvenArgs e);
//主持人类
public class Host
{
public event QuestionHander AnswerQuestion;
public string Name { get; set; }
public string Question { get; set; }//问题
//触发事件方法
public void Start()
{
Console.WriteLine("大家好,我是:" + Name);
Console.WriteLine("今天的问题是:" + Question);
Console.WriteLine("请各位嘉宾开始回答");
//通知所有嘉宾回答问题
if (AnswerQuestion != null)
{
AnswerQuestion(this, new QuestionEvenArgs { QuestionMes = Question });
}
}
}
//嘉宾抽象类--父类
public abstract class Guest {
public string Name { get; set; }
public abstract void Answer(object sendr, QuestionEvenArgs e);
}
public class GuestA : Guest
{
public override void Answer(object sendr, QuestionEvenArgs e)
{
Console.WriteLine(Name + ",这个问题是我考虑一下!");
}
}
public class GuestB : Guest
{
public override void Answer(object sendr, QuestionEvenArgs e)
{
Console.WriteLine(Name + ",这个问题我知道!");
}
}
public class GuestC : Guest {
public override void Answer(object sendr, QuestionEvenArgs e)
{
Host h = sendr as Host;
Console.WriteLine(h.Name+",这个问题是"+e.QuestionMes+",我知道答案,但是我不告诉你");
}
}
public class Test {
public static void Main(string[] args) {
Host h = new Host { Name = "李咏", Question = "伦敦奥运什么时候开始?" };
GuestA a = new GuestA { Name = "AAA" };
GuestB b = new GuestB { Name = "BBB" };
GuestC c = new GuestC { Name = "CCC" };
h.AnswerQuestion += new QuestionHander(a.Answer);
h.AnswerQuestion += new QuestionHander(b.Answer);
h.AnswerQuestion += new QuestionHander(c.Answer);
h.Start();
}
}
}
[解决办法]