在.net中传递事件

本文关键字:事件 net | 更新日期: 2023-09-27 18:01:39

我很抱歉发布了这么一大块代码,但在这种情况下,我觉得它会更容易理解这个问题,尽管这个问题可能非常简单(我希望有一个同样简单的答案)。

我正在玩事件和委托。在Main方法中,我有代码

traffic.PutInGarage(g);

这意味着我正在传递车库类的引用(参见下面的代码)。这是您期望事件传递的方式吗?我不知道为什么,我不能解释为什么,感觉不对,好像我错过了什么地方。

再次抱歉发布所有控制台应用程序代码,但它可能更容易。

using System;
using System.Collections.Generic;
namespace DemoProejct
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Garage g = new Garage();
            g.NewCarEvent += new Garage.NewCarDelegate(GarageCount);
            Traffic traffic = new Traffic();
            //SHOULD I BE PASSING THE Garage object here?
            traffic.PutInGarage(g);
            Console.WriteLine("Garage is now closed");
            Console.ReadKey();
        }
        private static void GarageCount(string cars, string s)
        {
            Console.WriteLine(string.Format("{0} {1}", cars, s));
            System.Threading.Thread.Sleep(2000);
        }
    }
    public class Traffic
    {
        public void PutInGarage(Garage g)
        {
            List<Vehicle> all = GetVehicles();
            Vehicle modelWeFix = new Vehicle() { Make = "Mazda", Model = "6", Year = "2012" };
            int i = 1;
            foreach (IEquatabled<Vehicle> item in all)
            {
                if (item.EqualsTo(modelWeFix))
                {
                    g.CarsInGarage = i;
                    i++;
                }
            }
        }
   private List<Vehicle> GetVehicles()
   {
        Car carMazda = new Car() { Make = "Mazda", Model = "6", Year = "2012" };
        Car carFord = new Car() { Make = "Ford", Model = "Sport", Year = "2002" };
        Car carUnknown = new Car() { Make = "Mazda", Model = "5", Year = "2012" };
        Bike mazdaBike = new Bike() { Make = "Mazda", Model = "6", Year = "2012" };
        IEquatabled<Vehicle> unknownBike = mazdaBike;
        List<Vehicle> all = new List<Vehicle>();
        all.Add(carMazda);
        all.Add(carFord);
        all.Add(carUnknown);
        all.Add(mazdaBike);
        return all;
   }
}
    public class Garage
    {
        public delegate void NewCarDelegate(string numberOfCars, string message);
        public event NewCarDelegate NewCarEvent;
        private int _carsInGarage;
        public int CarsInGarage
        {
            get { return _carsInGarage; }
            set
            {
                if (NewCarEvent != null)
                {
                    _carsInGarage = value;
                    NewCarEvent(value.ToString(), " cars in the garage.");
                }
            }
        }
        public Garage()
        {
            CarsInGarage = 0;
        }
    }
    public class Vehicle : IEquatabled<Vehicle>
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public string Year { get; set; }
        public virtual int Wheels { get; set; }
        //Implementation of IEquatable<T> interface 
        public bool EqualsTo(Vehicle car)
        {
            if (this.Make == car.Make && this.Model == car.Model && this.Year == car.Year)
                return true;
            else
                return false;
        }
    }
    public class Car : Vehicle
    {}
    public class Bike : Vehicle
    {}
    interface IEquatabled<T>
    {
        bool EqualsTo(T obj);
    }
}

在.net中传递事件

不要把它传递给每个要放入车库的调用,但是用车库创建一个Traffic实例会更有意义吗?

        Garage g = new Garage();
        g.NewCarEvent += new Garage.NewCarDelegate(GarageCount);
        Traffic traffic = new Traffic(g); // pass your garage here.
        traffic.PutInGarage();

评论回复:

方法应该在Garage对象上吗?Garage.StoreTraffic(traffic)——实现似乎是倒退的。的交通不应该对车库的行为负责... .- - - - - -charles Feb 28 at 15:25

这表明我确实需要传递一个实例作为参数,但也有助于修复我的实现!