当该类作为参数传递给静态方法时,我正在尝试从该类的派生类访问方法的基本实现。在下面的示例中,我希望控制台在调用Child.WriteSomthing()时写“ This is Parent ...”。
按现状,它递归地调用Child.WriteSomthing()。有什么办法可以做到这一点?
public class Parent
{
public virtual void WriteSomething()
{
Console.WriteLine("This is Parent...");
}
public class Child : Parent
{
public override void WriteSomething()
{
Helpers.SomeHelper(3, this);
}
}
public static class Helpers
{
public static void SomeHelper(int i, Child child)
{
switch (i)
{
case 1:
Console.WriteLine("Child 1...");
break;
case 2:
Console.WriteLine("Child 2...");
break;
default: (Parent)child.WriteSomething();
}
}
}