OO, Inheritance and Queue



I have a queue that holds a list of objects with a common base type. As
a simple and contrived example, I have a common type of transportation
methods and derived classes (with there own individual methods which
extend the common base class) of Car, Bicycle and Plane:-

class Car : Transport { OpenWindow(); }
class Bicycle : Transport { RingBell(); }
class Plane : Transport { TakeNap(); }

I want to add each of these classes to the buffer. So I can enqueue
with:

Queue transportQueue;
transportQueue.Enqueue(MyCar);
transportQueue.Enqueue(MyBicycle);
transportQueue.Enqueue(MyPlane);

Now this is where I have got concerns. When I dequeue I do not know
what type has been dequeued.

Transport transportMethod;
transportMethod = (Transport) transportQueue.Dequeue();

When I dequeue, I know that it is a transport class but I do not know
if it is a Car, Bicycle or Plane.

I suppose I could use:
switch (typeof(transportMethod))
{
case Car:
break;
case Bicycle:
break;
case Plane:
break;
}

But this does not seem very Object Oriented and not very maintainable.
Is there a better way of doing this?

.



Relevant Pages

  • Re: OO, Inheritance and Queue
    ... I think one of my concerns is how the classes Car, Bicycle and Plane ... If you have the Car Bicycle or Transport objects already, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Another car smashes a building.
    ... So a car hitting a wall will cause more damage than a bicycle. ... "class" based on their mode of transport. ... Can of worms - what every fisherman wants. ...
    (uk.transport)
  • Re: Another car smashes a building.
    ... So a car hitting a wall will cause more damage than a bicycle. ... "class" based on their mode of transport. ...
    (uk.transport)
  • Re: reducing food miles
    ... if they used a cycle instead of a car for shopping. ... And how do you transport a shopping-trolley load of food on a bicycle? ...
    (uk.transport)
  • Re: OO, Inheritance and Queue
    ... you can declare a virtual/abstract method in the Transport class called ... And in car, you want to declare OpenWindowas a private method, and then you want to override this method. ... Bicycle and Plane implement this interface (or ... If you have the Car Bicycle or Transport objects already, ...
    (microsoft.public.dotnet.languages.csharp)

Loading