DoSomething(Car car);
DoSomething(Bike bike);
public class Car : Vehicle {}
public class Bike : Vehicle {}
public abstract class Vehicle {}
void run(Vehicle vehicle) {
DoSomething(vehicle);
}
This seems like a simple problem, but I have problems. DoSomething (Vehicle vehicle) does not exist, therefore DoSomething (vehicle) gives an error even if the car is "guaranteed" like a car or a bicycle. How can I convince the compiler that a “car” is a bicycle or a car, so that DoSomething can be started?
Of course, I could have a different method line by line
DoSomething(Vehicle vehicle)
{
if(vehicle is Car) ... etc
}
But of course, is there a cleaner approach?
EDIT / CLARITY
The motivation for placing this code in the manager class, rather than the presence of DoSomething () in the Vehicle class, is that each car should have access to different parts of the program. For instance:
DoSomething(Car car) {
motorwayInfo.CheckMotorwayStatus();
}
DoSomething(Bike bike) {
cycleInfo.CheckCyclePathStatus();
}
, , , Car - cycInfo, , - motorWayInfo. , DoSomething Vehicle, , :
DoSomething(CycleInfo cycleInfo, MotorwayInfo motorwayInfo)
DoSomething(InfoManager infoManager)
, , . ?