我有以下代码。我需要隐藏界面的一个功能。部分受保护的接口,但没有抽象类
interface IOne
{
int FunctionOne();
}
interface ITwo
{
double FunctionTwo();
}
interface IInterfaceToImplement : IOne, ITwo
{
void AFunctionToImplement();
}
public abstract MyBaseClass : TheVeryHeavyBaseClass<T, IInterfaceToImplement>, IInterfaceToImplement
{
public abstract void AFunctionToImplement(); // How to force this one to be protected?
public virtual int FunctionOne() { return 0; }
public virtual double FunctionTwo() { return 0.0; }
}
public MyConcreteClass : MyBaseClass
{
public override void AFunctionToImplement(); // How to force this one to be protected?
}
正如你所看到的我有基类。我需要隐藏AFunctionToImplement()
。我有差劲的课程设计吗?有关如何保护函数不被调用的任何建议?
编辑。在评论中回答Pavel Minaev的问题。
我需要每个具体类实现IInterfaceToImplement中的函数列表。另外我需要每个具体类都能够存储IInterfaceToImplement类型的类。 这是树形数据存储。存储的每个“分支”都必须执行与其他分支相同的操作。但除了“根”和其他“分枝”之外,其他人都不得不称之为这些操作。
EDIT2我的解决方案。
谢谢马修雅培和帕维尔米娜夫。我终于意识到我的问题 - 这是大脑。 :)
不,我在开玩笑。 :)问题是 - 我想到了根分支类和同一分支。现在我明白了 - 根不应该执行IInterfaceToImplement
。查看解决方案:
public class MyRootClass : IOne, ITwo
{
private IInterfaceToImplement internalData = new MyConcreteClass();
public int FunctionOne() { return this.internalData.FunctionOne(); }
public double FunctionTwo() { return this.internalData.FunctionTwo(); }
}
你究竟想要达到什么目的?接口是定义的公共契约,但将接口定义保密是没有意义的。有时候让特定的实现是半私有的是有道理的,但是因为人们总是可以获得对同一个对象的接口类型引用,并通过它调用接口方法,所以接口实现永远也不可能真正是私有的。所以...你想要做什么? – 2010-07-05 08:04:17