c# - 接口(interface)实现而不透露实现该接口(interface)的类

标签 c# interface

嗨,我们如何在实时场景中实现界面?

这就是我的情况

我制作了一个接口(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)类。

谢谢

最佳答案

我建议:

  1. 阅读 dependency injection以及它如何帮助您以松散耦合的方式轻松解决依赖关系。
  2. 界面名称“IPayPal”恕我直言,这不是一个很好的名字。它是针对某一支付提供商的。假设明天您想要实现另一种非 paypal 的付款方式,但您想使用相同的接口(interface)。我认为该名称应该是通用的,如“IPaymentProvider”,并且当前的实现是 PayPal(但使用该接口(interface)的其他类不应该关心或了解这一点)。

祝你好运!

关于c# - 接口(interface)实现而不透露实现该接口(interface)的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9206362/

相关文章:

c# - 通过使用 linq 按字母顺序分组来计算电影

.net - 为什么HashSet <T>不实现ICollection?

c# - ASP.NET MVC 中的接口(interface)参数

javascript - Typescript 提示界面中不存在私有(private)属性

go - 接口(interface)及其实现的包/目录结构

java - 接口(interface)的Spring依赖注入(inject)

c# - 无法使用 RESP 在 Redis 服务器上执行命令

c# - 从两个 List<> 生成随机数

c# - Unity AudioSource 无法播放剪辑

c# - 无法使用 selenium 网络驱动程序 : ElementNotVisibleException 单击 Angular-JS 按钮