![](/img/trans.png)
[英]Perform a 'protected abstract override' for an abstract class which implements a generic interface
[英]Partially protected interface but without abstract class
我有以下代码。 而且我需要隐藏界面的一项功能。
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我的解决方案。
感谢Matthew Abbott和Pavel Minaev。 我终于意识到我的问题-这是大脑。 :)
不,我在开玩笑。 :)问题是-我认为根类和分支类与同一分支相同。 现在我明白了-根目录不应该实现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(); }
}
我建议使用显式接口实现:
void IInterfaceToImplement.AFunctionToImplement();
...但是这不允许您在子类中公开和实现该方法,并且仍然将其隐藏。 在这种情况下,您可能需要重新考虑您的班级设计。
最好的选择是,它类似于以下内容:
public interface IMyInterface
{
void DoWork();
}
public abstract class MyInterfaceBase : IMyInterface
{
/// <summary>
/// Forced implementation.
/// </summary>
protected abstract void DoWork();
/// <summary>
/// Explicit implementation.
/// </summary>
void IMyInterface.DoWork()
{
// Explicit work here.
this.DoWork();
}
}
如果从IMyInterface引用而不是MyInterfaceBase引用调用该方法,则仍然存在公开公开DoWork的问题。 您根本无法解决这个问题。 如果我执行以下操作:
MyInterface first = new MyInterface(); // Let's assume I have implemented MyInterface : MyInterfaceBase
first.DoWork(); // Compile error, can't access method here.
鉴于:
IMyInterface second = new MyInterface();
second.DoWork(); // No problem.
你看到这个问题了吗?
只能为公共成员定义接口。 如果要只将方法定义为受保护的方法,则不要为其创建接口,而只需将其创建为抽象基类的受保护抽象成员即可。 即使您使用显式接口,如果该实例属于相关接口类型,它仍然可以公开访问。
public abstract MyBaseClass<T> : TheVeryHeavyBaseClass<T>
{
// remove the interface the defined this
protected abstract void AFunctionToImplement();
// other code
}
每当您编写接口时,您都在隐式声明一个实现该接口的任何类的公共视图,因此尝试隐藏其方法根本没有任何意义。 您可以将AFunctionImplementation移出接口,并将其保留在MyBase类中。 MyBase类的任何实现仍然可以访问它。
在这种情况下,您还可以使MyBase类直接实现IOne和ITwo。
在名称中使用“ Implementation”一词作为功能的一部分将是避免将其放在接口中的一个很好的提示,因为接口通常被用作将用法与实现细节隔离开的手段。
也许您可以在抽象类中将方法虚拟化,然后引发异常
/// <summary>
/// Available only in derived classes
/// </summary>
public virtual void AFunctionToImplement2()
{
throw new ProtectedMethodException("Please do not call this method in the base class:) ");
}
我不知道这是否是一个愚蠢的解决方案,但是至少您不允许用户使用该方法,即使该方法是公开的也是如此。
public class MyRootClass : IOne, ITwo
{
private IInterfaceToImplement internalData = new MyConcreteClass();
public int FunctionOne() { return this.internalData.FunctionOne(); }
public double FunctionTwo() { return this.internalData.FunctionTwo(); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.