c# - 如何防止暴露任何其他接口(interface)方法?

标签 c# asp.net .net interface

我有一个如下所示的界面

public interface I1 
{
public int add(int a , int b);

public int subtract (int a, int b);

}

public class Myclass : I1
{
//here I can access both the methods of interface I1
//add and subtract but i want to expose only add not subtract method
//How can I achieve this? 

}

我怎样才能只公开特定方法并阻止其他方法。

最佳答案

对于这种需求,请选择抽象类而不是接口(interface),因为在接口(interface)中所有方法默认都是公共(public)的,这是抽象类和接口(interface)之间的区别之一。

在interface中不能放private和public这样的修饰符,所有的方法默认都是public的。

关于c# - 如何防止暴露任何其他接口(interface)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14996215/

相关文章:

c# - MVC Controller 上的异步与同步基准测试有问题

c# - 如何找到引用错误版本程序集的内容并修复它?

c# - 使用反射在谓词中引用类的属性

.net - 指定索引文档集合排序

c# - Web 窗体模型绑定(bind) : How to omit binding for not visible control?

c# - 使用 HTML Agility Pack 仅获取网页文本?

c# - 在代理后面连接到 Azure Batch - 操作返回无效状态代码 'Forbidden'

c# - 从 ViewModel 绑定(bind)到反向 ObservableCollection

c# - 更改 SSL 网站的 Web.Config

ASP.NET Windows 身份验证路由不同组