using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BoiledEventArgs boil = new BoiledEventArgs(); //实例化
Ev e = new Ev();
e.numberEvent += new Ev.NumBerDelegate(boil.AAA); //为事件添加方法
e.numberEvent += boil.BBB;
e.numberEvent += new BoiledEventArgs().BBB;
e.OnEvent();
Console.ReadLine();
}
}
public class BoiledEventArgs
{
public void AAA()
{
Console.WriteLine("AAA");
}
public void BBB()
{
Console.WriteLine("BBB");
}
}
public class Ev
{
public delegate void NumBerDelegate();
public event NumBerDelegate numberEvent; //生命事件
public virtual void OnEvent()
{
if (numberEvent != null)
{
numberEvent(); //在这里面触发方法-不能在外部调用
Console.WriteLine("numberEvent != null");
}
}
}
}
C# 事件的基础
最新推荐文章于 2024-04-23 19:57:46 发布