每个继承类的方法都需要不同类型的参数。在这种情况下,我应该如何在接口方法中定义参数以使所有子方法都可以接受?
public interface IPayment
{
void MakePayment(OrderInfo orderInfo); // !!
void MakeRefund (OrderInfo orderInfo); // !!
}
public class OrderInfo
{
protected string OrderNo {get; set;}
protected string CustomerNo { get; set;}
protected decimal Amount {get; set;}
}
public class CreditCardPaymentInfo : OrderInfo
{
string CCNum {get; set;}
string ExpDate { get; set;}
}
public class GooglePaymentInfo : OrderInfo
{
string GoogleOrderID {get; set;}
}
public class PaypalPaymentInfo : OrderInfo
{
string PaypalID {get; set;}
}
public void MakePayment()
{
IPayment paymentModule;
// Get Order Info
if(orderType == "Paypal"){
paymentModule = new PaypalPayment();
PaypalPaymentInfo orderInfo = new PaypalPaymentInfo();
orderInfo.PaypalID = "TEST";
}else if(orderType == "Google"){
paymentModule = new GooglePayment();
GooglePaymentInfo orderInfo = new GooglePaymentInfo();
orderInfo.GoogleOrderID = "TEST";
}else{
paymentModule = new CreditCardPayment();
CreditCardPaymentInfo orderInfo = new CreditCardPaymentInfo();
orderInfo.CCNum = "1111111111111111";
orderInfo.ExpDate = "11/11";
}
orderInfo.OrderNo = "123";
orderInfo.CustomerNo = "ABC";
orderInfo.Amount = 12.20m;
paymentModule.MakePayment();
}
它发生错误:
错误 1 'com.WebUI.Models.CreditCardPaymentInfo' 没有实现接口成员 'com.WebUI.Models.IPaymentProcess.makeRefund(WebUI.Models.RefundModel)'
[编辑]
哦,我忘记了我的模型代码,是这样的,
public class CreditCardPayment: IPayment
{
public void MakePayment(CreditCardPaymentInfo creditCardPaymentInfo ){...}
//The parameter type is NOT OrderInfo
//public void MakePayment(OrderInfo orderInfo){...}
public void MakeRefund(CreditCardPaymentInfo creditCardPaymentInfo ){...}
}
但在 CreditCardPayment 案例中,我需要传递CreditCardPaymentInfo 参数而不是仅包含公共字段的OrderInfo 。