class Person
{
// 委托如果是public修饰,就可以在类的外部调用
public delegate void behaviourDelegate();// 委托
// 事件只能在定义该事件的类的内部调用,不管是public修饰,还是private修饰,都只能在类的内部调用
public event behaviourDelegate behaviourEvent;// 事件
public void notify()
{
if (this.behaviourEvent != null)
{
this.behaviourEvent();
}
}
public void sayEvent()
{
Console.WriteLine("sayEvent");
}
public void talkEvent()
{
Console.WriteLine("talkEvent");
}
public void walkEvent()
{
Console.WriteLine("walkEvent");
}
}
class Start
{
static void Main(string[] args)
{
var per = new Person();
per.behaviourEvent += per.sayEvent;
per.behaviourEvent += per.talkEvent;
per.behaviourEvent += per.walkEvent;
per.notify();
Console.ReadLine();
}
}
C#_事件的使用
最新推荐文章于 2023-03-29 10:15:09 发布