加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 界面的私有成员

发布时间:2020-12-16 00:24:17 所属栏目:百科 来源:网络整理
导读:如果在我的程序中我有接口,那么它的所有成员都是隐式公开的.在实现该接口的类中,我必须公开该成员(属性). 是否可以将其设为私有? 解决方法 Is it any way to make an interface implementation private? 不完全私有 – 接口表示一组公共方法和属性.没有办法
如果在我的程序中我有接口,那么它的所有成员都是隐式公开的.在实现该接口的类中,我必须公开该成员(属性).

是否可以将其设为私有?

解决方法

Is it any way to make an interface implementation private?

不完全私有 – 接口表示一组公共方法和属性.没有办法让接口实现私有化.

你可以做的是明确实现:

public interface IFoo
{
   void Bar();
}

public class FooImpl
{
    void IFoo.Bar()
    {
       Console.WriteLine("I am somewhat private.")
    }

    private void Bar()
    {
       Console.WriteLine("I am private.")
    }

}

现在调用IFoo.Bar()的唯一方法是通过接口显式:

FooImpl f = new FooImpl();
f.Bar();   // compiler error
((IFoo)f).Bar();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读