嗨,我们如何在实时场景中实现界面?
这就是我的情况
我制作了一个接口(interface) IPayPal,它有 2 个方法
void SaleTransaction();
void VoidTransaction();
现在我有一个 PayPal 类来实现此服务。
class PayPal:IPayPal{
public void SaleTransaction(){
// Implementation happens here
}
public void VoidTransaction(){
// Implementation happens here
}
}
现在我有一项服务请求 PayPal 提供服务
可以说
class Service{
IPayPal pp=null;
static void Main(){
pp=new PayPal();
//Now i do not want to expose all the methods in my class PayPal
// is there any other way to just show pp.SaleOneTransaction() method?? i donot want the //PayPal class to be present in this Program..
//Please tell me how to acheive this.
}
}
即请告诉我一种方法,可以在不透露实现该接口(interface)的类的情况下初始化我的接口(interface)类。
谢谢
最佳答案
我建议:
- 阅读 dependency injection以及它如何帮助您以松散耦合的方式轻松解决依赖关系。
- 界面名称“IPayPal”恕我直言,这不是一个很好的名字。它是针对某一支付提供商的。假设明天您想要实现另一种非 paypal 的付款方式,但您想使用相同的接口(interface)。我认为该名称应该是通用的,如“IPaymentProvider”,并且当前的实现是 PayPal(但使用该接口(interface)的其他类不应该关心或了解这一点)。
祝你好运!
关于c# - 接口(interface)实现而不透露实现该接口(interface)的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9206362/