我有一个关于派生,多态和方法签名的问题
我有课
public abstract class PaymentMethod
{
public abstract float GetSalary (Employee employee,Vehicule vehicule)
}
Run Code Online (Sandbox Code Playgroud)
和另外两个人
public class MarinePaymentMethod : PayementMethod
{
public override float GetSalary (Sailor sailor,Boat boat)
}
public class AirPaymentMethod : PayementMethod
{
public override float GetSalary (Pilot pilot,Plane plane)
}
Run Code Online (Sandbox Code Playgroud)
我们还假设:
public class Sailor : Employee{}
public class Pilot : Employee{}
public class Boat: Vehicule{}
public class Plane: Vehicule{}
Run Code Online (Sandbox Code Playgroud)
所以,"问题"是这个代码不能编译,因为签名不一样.
我强制保留基本签名GetSalary(员工员工,Vehicule vehicule)
然后我必须投在派生付款方式,这样我可以使用的特定成员Pilot,Sailor,Boat,Plane在这些具体的付款方式.
我的第一个问题是:连续投射不是难闻的气味吗?
我的第二个问题是:如何制作更优雅的代码设计?我在考虑Generics,并创建一个这样的类:
public abstract class …Run Code Online (Sandbox Code Playgroud)