界面的私有成员 [英] Private members of the interface
本文介绍了界面的私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果在我的程序中我有该接口,则它的所有成员都是隐含的 public .在实现该接口的类中,我也必须将该成员(属性)设为 public .
If in my program I have the interface, then all it's members are public implicitly. And in the class, which implements that interface, I must make that members (properties) public too.
有什么办法使其成为私有?
推荐答案
是否可以将接口实现私有化?
Is it any way to make an interface implementation private?
不是完全的 私有-接口表示一组公共的方法和属性.无法将接口实现私有化.
Not completely private - an interface represents a public set of methods and properties. There is no way to make interface implementations private.
您可以 做的是使实现 explicit :
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()
的唯一方法是通过接口显式:
Now the only way to call IFoo.Bar()
is explicitly through the interface:
FooImpl f = new FooImpl();
f.Bar(); // compiler error
((IFoo)f).Bar();
这篇关于界面的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文