简体   繁体   English

C#,使用接口

[英]C#, Using Interface

I'm trying to use Interface class and I have a question about Interface method parameters. 我正在尝试使用Interface类,我对Interface方法参数有疑问。

I have an Interface class to make child class use specific method. 我有一个Interface类来使子类使用特定的方法。 but the child classes need different number of parameters for the method. 但是子类需要不同数量的参数。

Example, 例,

public interface IPayment
{
  void MakePayment();
}

and define the MakePayment method in child classes. 并在子类中定义MakePayment方法。

public class PayPay : IPayment
{
  public void MakePayment(string a); // it needs only one parameter
}

public class Google : IPayment
{
  public void MakePayment(string a, int b); // it needs two parameters.
}

Like above case, How can I modify my interface class? 像上面的情况一样,我该如何修改我的接口类?

Thank you! 谢谢!

一个干净的方法可以是简单地使用PaymentParameters类并使用一个名为public void MakePayment(PaymentParameters params);

Change your interface to use a PaymentParameters class that contains all the parameters each different service might need. 更改您的界面以使用PaymentParameters类,该类包含每个不同服务可能需要的所有参数。

public interface IPayment 
{ 
   void MakePayment(PaymentParameters p);   
} 

public class PaymentParameters{
    public string A { get; set; }
    public int B { get; set; }
}

Your Google and PayPay implementations will use only the needed parameters from PaymentParameters . 你的GooglePayPay实施将使用只从需要的参数PaymentParameters

Well, the principe of the interface is to not know about the implementation. 那么,接口的原理是不知道实现。 So the caller of the interface won't know if it is a Google of PayPay sub class and will always call the same method. 因此,界面的调用者将不知道它是否是PayPay子类的Google并且将始终调用相同的方法。

But if you have more complex parameters, you should use IPaymentParameters and a method with this prototype 但是如果你有更复杂的参数,你应该使用IPaymentParameters和一个方法与这个原型

public interface IPayment
{
     void MakePayment(IPaymentParameters parameters);
}

And two implementations GooglePayementParameters and PayPayPaymentParameters 并实现了两个GooglePayementParametersPayPayPaymentParameters

Do your PayPay and Google classes represent the data necessary to describe a payment? 您的PayPayGoogle课程是否代表描述付款所需的数据? Normally, a Payment class should represent a payment. 通常,付款类应代表付款。 If the class's job is to process payments, it should probably have a name like PaymentProcessor , and its interface something like IPaymentProcessor (or indeed IPaymentService , or something similar). 如果班级的工作是处理付款,它应该有一个像PaymentProcessor这样的名称,其接口类似于IPaymentProcessor (或者甚至是IPaymentService ,或者类似的东西)。

If the payment classes represent actual payments, then the class should not need any parameters to its MakePayment() method; 如果支付类代表实际支付,那么该类不应该需要MakePayment()方法的任何参数; instead, it would rely on the instance data to describe the payment being made. 相反,它将依赖实例数据来描述正在进行的付款。

Alternatively, you could have something like this (still using Payment to describe the payment itself): 或者,您可能会有这样的事情(仍使用Payment来描述付款本身):

interface IPaymentProcessor<T> where T : IPayment
{
    void ProcessPayment(T payment);
}
class PayPayPaymentProcessor : IPaymentProcessor<PayPay>
{
    void ProcessPayment(PayPay payment) { /* some implementation here */ }
}
class PayPayPaymentProcessor : IPaymentProcessor<Google>
{
    void ProcessPayment(Google payment) { /* some implementation here */ }
}

I would probably name the classes PayPayPayment and GooglePayment so the names more clearly represent the type: 我可能会将课程命名为PayPayPaymentGooglePayment因此名称更清楚地表示类型:

class PayPayPaymentProcessor : IPaymentProcessor<PayPayPayment>
{
    void ProcessPayment(PayPayPayment payment) { /* some implementation here */ }
}
class PayPayPaymentProcessor : IPaymentProcessor<GooglePayment>
{
    void ProcessPayment(GooglePayment payment) { /* some implementation here */ }
}

Note that this is very similar to the approach other suggested of using a PaymentParameters class, but it adheres more closely to the single responsibility principle. 请注意,这与其他建议使用PaymentParameters类的方法非常相似,但它更贴近单一责任原则。 In Brian Cauthon's answer, the PaymentParameters class has to hold the union of all possible parameters for any type of payment; 在布莱恩Cauthon的回答, PaymentParameters类必须适用于任何类型的支付的所有可能的参数的结合; here, the parameter types can (and should) be specific to the needs of the payment they represent. 在这里,参数类型可以(并且应该)特定于它们所代表的付款需求。

You can either define two interfaces, or use the following; 您可以定义两个接口,也可以使用以下接口;

public interface IPayment
{
    void MakePayment(string a, int b=0);
}

When you only pass the string parameter, the method will set b to 0; 当您只传递字符串参数时,该方法将b设置为0; you can just ignore it. 你可以忽略它。

If you use .NET 4.0 than you can give your Interface-Method default-values 如果使用.NET 4.0,则可以使用Interface-Method默认值

public interface ITest{
void Function(stirng s1, string s2 = "");
}

Straight from MSDN - 直接来自MSDN -

An interface contains only the signatures of methods, delegates or events 接口仅包含方法,委托或事件的签名

The signature of a method consists of the name of the method and the type and kind (value, reference, or output) of each of its formal parameters , considered in the order left to right 方法的签名包括方法的名称以及每个形式参数的类型和种类(值,引用或输出),按从左到右的顺序考虑

If you don't provide the implementation for the method (including the type and kind of each parameter), then you haven't really 'implemented' the Interface. 如果您没有提供方法的实现(包括每个参数的类型和类型),那么您还没有真正“实现”接口。

Good luck! 祝好运! :) :)

Glen 狭谷

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM