// Cannot change source code
class Base
{
public virtual void Say()
{
Console.WriteLine("Called from Base.");
}
}
// Cannot change source code
class Derived : Base
{
public override void Say()
{
Console.WriteLine("Called from Derived.");
base.Say();
}
}
class SpecialDerived : Derived
{
public override void Say()
{
Console.WriteLine("Called from Special Derived.");
base.Say();
}
}
class Program
{
static void Main(string[] args)
{
SpecialDerived sd = new SpecialDerived();
sd.Say();
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
来自Special Derived.
来自Derived./*这不是预期的*/
从Base调用.
如何重写SpecialDerived类,以便不调用中产阶级"Derived"的方法?
更新: 我想继承Derived而不是Base的原因是Derived类包含许多其他实现.既然我不能在 …
class A
{
public override int GetHashCode()
{
return 1;
}
}
class B : A
{
public override int GetHashCode()
{
return ((object)this).GetHashCode();
}
}
new B().GetHashCode()
Run Code Online (Sandbox Code Playgroud)
这会溢出堆栈.我怎么能叫Object.GetHashCode()的B.GetHashCode()?
编辑:B现在继承自A.