以下代码取自我正在研究的书,他们说输出的后半部分应该是大写的,这要归功于OnCarEngineEvent2。然而,当我编译程序时,情况并非如此。为什么它不起作用,需要改变什么?感谢。
public class Car
{
// Internal state data.
public int CurrentSpeed { get; set; }
public int MaxSpeed { get; set; }
public string PetName { get; set; }
// Is the car alive or dead?
private bool carIsDead;
// Class constructors.
public Car() { MaxSpeed = 100; }
public Car(string name, int maxSp, int currSp)
{
CurrentSpeed = currSp;
MaxSpeed = maxSp;
PetName = name;
}
// 1) Define a delegate type.
public delegate void CarEngineHandler(string msgForCaller);
// 2) Define a member variable of this delegate.
private CarEngineHandler listOfHandlers;
// 3) Add registration function for the caller.
public void RegisterWithCarEngine(CarEngineHandler methodToCall)
{
// listOfHandlers = methodToCall;
// listOfHandlers += methodToCall;
// listOfHandlers += methodToCall;
if (listOfHandlers == null)
listOfHandlers = methodToCall;
else
Delegate.Combine(listOfHandlers, methodToCall);
}
// 4) Implement the Accelerate() method to invoke the delegate’s
// invocation list under the correct circumstances.
public void Accelerate(int delta)
{
// If this car is 'dead', send dead message.
if (carIsDead)
{
if (listOfHandlers != null)
listOfHandlers("Sorry, this car is dead...");
}
else
{
CurrentSpeed += delta;
// Is this car 'almost dead'?
if (10 == (MaxSpeed - CurrentSpeed)
&& listOfHandlers != null)
{
listOfHandlers("Careful buddy! Gonna blow!");
}
if (CurrentSpeed >= MaxSpeed)
carIsDead = true;
else
Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
}
}
}
class Program
{
static void Main( string[] args )
{
Console.WriteLine("***** Delegates as event enablers *****\n");
// First, make a Car object.
Car c1 = new Car("SlugBug", 100, 10);
c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
// This time, hold onto the delegate object,
// so we can unregister later.
Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
c1.RegisterWithCarEngine(handler2);
// Speed up (this will trigger the events).
Console.WriteLine("***** Speeding up *****");
for (int i = 0; i < 6; i++)
c1.Accelerate(20);
// We won't see the 'uppercase' message anymore!
Console.WriteLine("***** Speeding up *****");
for (int i = 0; i < 6; i++)
c1.Accelerate(20);
Console.ReadLine();
}
#region Delegate targets
// We now have TWO methods that will be called by the Car
// when sending notifications.
public static void OnCarEngineEvent( string msg )
{
Console.WriteLine("\n***** Message From Car Object *****");
Console.WriteLine("=> {0}", msg);
Console.WriteLine("***********************************\n");
}
public static void OnCarEngineEvent2( string msg )
{
Console.WriteLine("=> {0}", msg.ToUpper());
}
#endregion
}
答案 0 :(得分:2)
这是因为你的RegisterWithCarEngine
方法。目前,如果已附加了事件处理程序,则会调用Delegate.Combine(listOfHandlers, methodToCall)
,但不会更新listOfHandlers
。
使用listOfHandlers += (CarEngineHandler)Delegate.Combine(listOfHandlers, methodToCall);
这也可以简化为;
// 3) Add registration function for the caller.
public void RegisterWithCarEngine(CarEngineHandler methodToCall)
{
listOfHandlers += methodToCall;
}
但请记住这不会分离前一个处理程序,因此您将同时显示小写和大写。
答案 1 :(得分:0)
public void RegisterWithCarEngine(CarEngineHandler methodToCall)
{
if (listOfHandlers == null)
listOfHandlers = methodToCall;
else
listOfHandlers += (CarEngineHandler)Delegate.Combine(listOfHandlers, methodToCall);
}
答案 2 :(得分:-2)
if(listOfHandlers == null) listOfHandlers = methodToCall; 其他 listOfHandlers =(CarEngineHandler)Delegate.Combine(listOfHandlers,methodToCall); //然后+ = in with context;)