C # How to implement method version control?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.