【发布时间】:2015-03-26 11:17:59
【问题描述】:
是否可以隐藏或阻止继承的 protected virtual 或 protected abstract 方法,以便进一步继承的类无法访问它们?
例如:
class Base<T>
{
protected abstract T CreateInstance();
}
class Derivative : Base<Derivative>
{
protected sealed override Derivative CreateInstance()
{
return new Derivative();
}
}
class MoreDerivative : Derivative
{
public MoreDerivative()
{
// cannot access CreateInstance here
}
}
我不认为它是用语言实现的,但也许有注释或类似的东西。
【问题讨论】:
-
你可以让
Derivative成为Base<T>的包装器,而不是从它继承(选择要转发到子类的功能)。它会破坏继承链(显然),但任何其他方法都会破坏该契约。
标签: c# inheritance protected