「假設下面的代碼:從「base.base」類調用方法?
public class MultiplasHerancas
{
static GrandFather grandFather = new GrandFather();
static Father father = new Father();
static Child child = new Child();
public static void Test()
{
grandFather.WhoAreYou();
father.WhoAreYou();
child.WhoAreYou();
GrandFather anotherGrandFather = (GrandFather)child;
anotherGrandFather.WhoAreYou(); // Writes "I am a child"
}
}
public class GrandFather
{
public virtual void WhoAreYou()
{
Console.WriteLine("I am a GrandFather");
}
}
public class Father: GrandFather
{
public override void WhoAreYou()
{
Console.WriteLine("I am a Father");
}
}
public class Child : Father
{
public override void WhoAreYou()
{
Console.WriteLine("I am a Child");
}
}
我想打印‘‘對象
我怎麼能這樣做Child對象執行上一個方法’我是爺爺’,從」孩子基地.base「類?我知道我可以做它執行基本方法(它會打印」我是一個父親「),但我想打印」我是一個祖父「!如果有辦法做到這一點,是否在OOP設計中推薦?
注意:我不使用/將會使用這種方法,我只是想加強知識的繼承。