public delegate void DelegateClassHandle();
public class Employee
{
public event DelegateClassHandle PlayGame;
public void Games()
{
if (PlayGame != null)
{
PlayGame();
}
}
}
public class Admin
{
public void Notify()
{
System.Console.WriteLine("someone is playing game");
}
}
class program
{
public static void Main()
{
Employee employee = new Employee();
Admin admin = new Admin(); employee.PlayGame += new DelegateClassHandle(admin.Notify); //?? employee.Games();
Console.ReadKey();
}
}我就不明白employee.PlayGame += new DelegateClassHandle(admin.Notify)是不是相当于
employee.PlayGame =employee.PlayGame+ new DelegateClassHandle(admin.Notify)
“+=”运算符应该是这个意思吧?如果是的话,那又怎么理解那句代码呢?
public class Employee
{
public event DelegateClassHandle PlayGame;
public void Games()
{
if (PlayGame != null)
{
PlayGame();
}
}
}
public class Admin
{
public void Notify()
{
System.Console.WriteLine("someone is playing game");
}
}
class program
{
public static void Main()
{
Employee employee = new Employee();
Admin admin = new Admin(); employee.PlayGame += new DelegateClassHandle(admin.Notify); //?? employee.Games();
Console.ReadKey();
}
}我就不明白employee.PlayGame += new DelegateClassHandle(admin.Notify)是不是相当于
employee.PlayGame =employee.PlayGame+ new DelegateClassHandle(admin.Notify)
“+=”运算符应该是这个意思吧?如果是的话,那又怎么理解那句代码呢?
Employee.PlayGame 方法调用后 触发PlayGame事件 然后该事件委托给admin的Notify 方法
参考
employee.PlayGame =employee.PlayGame+ new DelegateClassHandle(admin.Notify)
“+=”运算符应该是这个意思吧?如果是的话,那又怎么理解那句代码呢? 事件委的绑定计算符。MS规定事件的委托就得用这个+=/-=。。LZ你Y的过不去后面哪句,就不符合规范。。