1

每个继承类的方法都需要不同类型的参数。在这种情况下,我应该如何在接口方法中定义参数以使所有子方法都可以接受?

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 。

4

1 回答 1

5
public interface IPayment<T> 
    where T: OrderInfo
{
  void MakePayment( T orderInfo );
  void MakeRefund ( T orderInfo );
}

然后:

public class CreditCardPayment
    : IPayment<CreditCardPaymentInfo>
{
    public void MakePayment( CreditCardPaymentInfo creditCardPaymentInfo ) {
        // ...
    }

    public void MakeRefund( CreditCardPaymentInfo creditCardPaymentInfo ) {
        // ...
    }
}

和:

public class CreditCardPaymentInfo
    : OrderInfo
{
    public string CCNum { get; set; }
    public string ExpDate { get; set; }
}
于 2012-11-03T13:47:22.133 回答