只是了解事件和委托,我有一个问题,关于使用相同的事件和委托输出不同的字符串,如果可能的话。举个例子。
class Program
{
static void Main(string[] args)
{
Publisher b = new Publisher();
Subscriber a = new Subscriber(b);
b.OnLog("12");
}
}
class Publisher
{
public delegate void LogHandler(string message);
public event LogHandler Log;
public void OnLog(string message)
{
//Fire the event - notifying all subscribers
if (Log != null)
Log(message);
}
}
class Subscriber
{
public Subscriber(Publisher pub)
{
//Where B is used - subscribe to it's public event
pub.Log += new Publisher.LogHandler(HandleSomethingHappening);
pub.Log += new Publisher.LogHandler(HandleSomethingHappeningStop);
}
public void HandleSomethingHappening(string message)
{
Console.WriteLine(message);
}
public void HandleSomethingHappeningStop(string message)
{
Console.WriteLine(message);
Console.ReadLine();
}
}
这里,两种方法
HandleSomethingHappening
和HandleSomethingHappeningStop
将输出“12”。如果我想要handleSomethingHappeningStop
输出,让我们说“24”,我是否需要创建一个与同一个委托关联的新事件来执行此操作?
所以例如
pub.LogOutput24 += new Publisher.LogHandler(HandleSomethingHappeningStop);
由于