For details, see section 11-7 "version control" in "C # And. NET Technology Platform Drill.
The example code is as follows:
Using system;
Class
{
Public void F1 ()
{
Console. writeline ("A. F1 ");
}
Public Virtual void F2 ()
{
Console. writeline ("A. F2 ");
}
Public Virtual void F3 ()
{
Console. writeline ("A. F3 ");
}
}
Class B:
{
Public Virtual void F1 ()
{
Console. writeline ("B. F1 ");
}
Public override void F2 ()
{
Console. writeline ("B. F2 ");
}
Public new void F3 ()
{
Console. writeline ("B. F3 ");
}
}
Class C: B
{
Public override void F1 ()
{
Console. writeline ("C. F1 ");
}
Public override void F2 ()
{
Console. writeline ("C. F2 ");
}
Public void F3 ()
{
Console. writeline ("C. F3 ");
}
}
Class app
{
Static void main (string [] ARGs)
{
A O1 = new ();
Console. writeline ("/////////////////////");
Console. writeline ("A O1 = new ()");
Console. writeline ("after we call the Methods :");
O1.f1 ();
O1.f2 ();
O1.f3 ();
//////////////////////////////////////// ////////////
B O2 = new C ();
Console. writeline ("/////////////////////");
Console. writeline ("B O2 = new C ()");
Console. writeline ("after we call the Methods :");
O2.f1 ();
O2.f2 ();
O2.f3 ();
//////////////////////////////////////// ////////////
A O3 = O2;
Console. writeline ("/////////////////////");
Console. writeline ("B O3 = new C ()");
Console. writeline ("after we call the Methods :");
O3.f1 ();
O3.f2 ();
O3.f3 ();
Console. Read ();
}
}
After thinking, you can compare the answer:
/////////////////////
A O1 = new ()
After we call the Methods:
A. F1
A. F2
A. F3
/////////////////////
B O2 = new C ()
After we call the Methods:
C. F1
C. F2
B. F3
/////////////////////
B O3 = new C ()
After we call the Methods:
A. F1
C. F2
A. F3
Summary: for Object-Oriented polymorphism, we often declare an object as a basic type, but initialize it as a derived type, so as to implement dynamic bonding of methods when calling methods of the basic class. To implement this feature, you must remember the principle that the methods in the basic class should use virtual modifiers, while the same methods in the derived class use override modifiers, this feature of polymorphism can be realized only through complete pairing.