實踐部分
我定義了以下四個類:
類定義
public class A
{
public virtual void Foo1()
{
Console.WriteLine("A.Foo1() was invoked.");
}
public void Foo2()
{
Console.WriteLine("A.Foo2() was invoked.");
}
}
public class B : A
{
public override void Foo1()
{
Console.WriteLine("B.Foo1() was invoked.");
}
public new virtual void Foo2()
{
Console.WriteLine("B.Foo2() was invoked");
}
}
public class C : B
{
public new void Foo1()
{
Console.WriteLine("C.Foo1() was invoked.");
}
}
public class D : C
{
public override sealed void Foo2()
{
Console.WriteLine("D.Foo2() was invoked.");
}
}
當運行如下代碼時,會打印出什麼?
運行這些代碼
C aD = new D();
A aC = new C();
aD.Foo1();
aD.Foo2();
aC.Foo1();
aC.Foo2();
結果是:
打印出的結果C.Foo1() was invoked.
D.Foo2() was invoked.
B.Foo1() was invoked.
A.Foo2() was invoked.