// Make a collection of Cars.
CarCollection myCars = new CarCollection();
myCars.AddCar(new Car("Rusty", 20));
myCars.AddCar(new Car("Zippy", 90));
foreach (Car c in myCars)
{
Console.WriteLine("PetName: {0}, Speed: {1}",
c.PetName, c.Speed);
}
Console.ReadLine();
}
Here you are creating a CarCollection type that contains only Car types. Again, you could
achieve a similar end result if you make use of the List type directly. The major benefit at this
point is the fact that you are free to add uniquely named methods (AddCar(), GetCar(), etc.) to the
CarCollection that delegate the request to the internal List.
Do'stlaringiz bilan baham: |