Virtual
We code every day but maybe we don't know the base theory clearly.The below example is about virtual method in C#,
what is the correct output?
Be frank with you,I don't comprehend the "Virtual methods" plenty,may be you will let me to look out
it in MSDN,but can you give the correct output and the pellucid explannation here? Thanks.
what is the correct output?
Be frank with you,I don't comprehend the "Virtual methods" plenty,may be you will let me to look out
it in MSDN,but can you give the correct output and the pellucid explannation here? Thanks.
using System;
class A
{
public virtual void F() { Console.WriteLine("A.F"); }
}
class B: A
{
public override void F() { Console.WriteLine("B.F"); }
}
class C: B
{
new public virtual void F() { Console.WriteLine("C.F"); }
}
class D: C
{
public override void F() { Console.WriteLine("D.F"); }
}
class Test
{
static void Main() {
D d = new D();
A a = d;
B b = d;
C c = d;
a.F();
b.F();
c.F();
d.F();
}
}
class A
{
public virtual void F() { Console.WriteLine("A.F"); }
}
class B: A
{
public override void F() { Console.WriteLine("B.F"); }
}
class C: B
{
new public virtual void F() { Console.WriteLine("C.F"); }
}
class D: C
{
public override void F() { Console.WriteLine("D.F"); }
}
class Test
{
static void Main() {
D d = new D();
A a = d;
B b = d;
C c = d;
a.F();
b.F();
c.F();
d.F();
}
}