2013-02-28 64 views
3

我很抱歉发布了这么大的一段代码,但在这种情况下,我觉得这会更容易理解这个问题,尽管问题可能很简单(同样简单的答案我希望)。在.NET中传递事件

我正在玩事件和代表。在我的Main方法中,我的代码是

traffic.PutInGarage(g); 

这意味着我传递了我的Garage类的引用(请参阅下面的代码)。这是你期望事件通过的方式吗?我不明白为什么,我无法解释为什么,感觉不对,就像我错过了某些地方的观点。

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

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); 
    } 
} 
+5

当然,该方法应该在'Garage'对象上? 'Garage.StoreTraffic(交通)' - 实施似乎倒退。交通不应该负责车库的行为... – Charleh 2013-02-28 15:25:14

+2

对不起,有什么问题吗? _这是你期望事件被传递的方式吗?_你不传递事件 - 你传递实例。 – 2013-02-28 15:25:37

+0

@Charleh - 我想你已经钉了它!我一直试图理解事件,完成事件,代表和继承,我不认为我能看到树木的木材!谢谢。 – Dave 2013-02-28 15:28:08

回答

0

回答评论:

肯定的方法应该是在车库对象? Garage.StoreTraffic(交通) - 实施似乎倒退。交通不应该对车库行为负责。 - Charleh 2月28日15:25

这说明我确实需要通过一个实例作为参数,还帮助解决我的实现!

0

不要将它传递到每个呼叫放入车库,但它是更有意义的创建与车库的交通实例。

 Garage g = new Garage(); 
     g.NewCarEvent += new Garage.NewCarDelegate(GarageCount); 

     Traffic traffic = new Traffic(g); // pass your garage here. 

     traffic.PutInGarage(); 
+0

你的答案表明,虽然可以通过Garage的一个实例。这是正确的,还是你做这只是因为我的代码呢? – Dave 2013-02-28 15:27:17

+0

这是我的答案,没有提到如前所述的潜在后向实现,但是如果您有一个依赖于其他类的类,请创建具有依赖性实例的类。 – hometoast 2013-02-28 15:28:23

+3

因此,交通只能与一个车库相关联?听起来像车库有垄断。 – 2013-02-28 15:28:27