簡體   English   中英

如何在接口類中設置正確的方法參數

[英]How to set right method's parameter in Interface class

每個繼承的類的方法都需要不同類型的參數。 在這種情況下,我應該如何在接口方法中定義參數以使所有子方法都能接受?

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

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; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM