0

下面的代码取自我正在学习的书,他们说输出的后半部分应该是大写的,这要归功于 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
}
4

3 回答 3

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;
}

但请记住,这不会分离先前的处理程序,因此您将同时显示小写和大写。

于 2013-01-15T16:18:03.100 回答
0
public void RegisterWithCarEngine(CarEngineHandler methodToCall)
{
    if (listOfHandlers == null)
        listOfHandlers = methodToCall;
    else
        listOfHandlers += (CarEngineHandler)Delegate.Combine(listOfHandlers, methodToCall);
}
于 2013-01-15T16:23:22.867 回答
-2

if (listOfHandlers == null) listOfHandlers = methodToCall; 否则 listOfHandlers = (CarEngineHandler)Delegate.Combine(listOfHandlers, methodToCall); //它会比 += 在上下文中更好;)

于 2015-03-29T23:10:37.723 回答