C# 事件调用
若一个类声明了一个事件,那个么只能从该类触发该事件。限制事件调用的原因是什么C# 事件调用,c#,C#,若一个类声明了一个事件,那个么只能从该类触发该事件。限制事件调用的原因是什么 using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication12 { delegate void Function(); class EventHandling { public event Function fun;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication12
{
delegate void Function();
class EventHandling
{
public event Function fun;
public void AddEvents(Function events)
{
fun += events;
}
public void Notify()
{
fun();
}
}
class Program
{
static void Main(string[] args)
{
EventHandling aEvtHandler = new EventHandling();
Function aFun = new Function(Display);
aEvtHandler.AddEvents(aFun);
aEvtHandler.Notify();
}
static void Display()
{
Console.WriteLine("in the display");
Console.Read();
}
}
}
触发事件只在定义它的类中有意义 如果我创建了一个Bike类,并且该类包含EngineStarted事件,那么该事件何时引发?当发生使发动机起动的事情时。因此,触发的事件意味着类的实例(一个Bike对象)向外部世界报告引擎已启动 另一个物体向世界报告某辆自行车的发动机启动,而自行车本身却没有先报告,这是毫无意义的