I have a question about Derivation , polymorphism and method signature
I have class
public abstract class PaymentMethod
{
public abstract float GetSalary (Employee employee,Vehicule vehicule)
}
and two others
public class MarinePaymentMethod : PayementMethod
{
public override float GetSalary (Sailor sailor,Boat boat)
}
public class AirPaymentMethod : PayementMethod
{
public override float GetSalary (Pilot pilot,Plane plane)
}
We assume also that :
public class Sailor : Employee{}
public class Pilot : Employee{}
public class Boat: Vehicule{}
public class Plane: Vehicule{}
So, the "problem" is that this code does not compile , because the signatures are not the same.
I am force to keep the base signature GetSalary(Employee employee, Vehicule vehicule)
and then I must cast in the Derived Payment Method, so that I can use the specific members of Pilot
, Sailor
, Boat
, Plane
in these specific payment method.
My first question is: Isn't it a bad smell to cast continuously?
My second question is: How to make a more elegant code Design ? I was thinking about Generics, and create a class like this:
public abstract class PaymentMethod<T, U> where T: Employee where U: Vehicule
But then in my code I realize I must put generic almost everywhere I use a payment mehod class and it make the code heavy. Any other solutions ?
Thanks a lot