我有以下代碼。我需要隱藏界面的一個功能。部分受保護的接口,但沒有抽象類
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