C # virtual functions

Source: Internet
Author: User

We can see virtual in many OOP languages such as C ++ and Java, and C # is no exception as a fully object-oriented language.

From the perspective of C # program compilation, what is the difference between a virtual function and other common functions? Generally, a function is statically compiled into the execution file during compilation. Its relative address remains unchanged during the program running, that is, it is completely written! While a virtual function is not statically compiled during compilation, its relative address is uncertain. It dynamically judges the function to be called Based on the object instance during the runtime, the class defined during the declaration is called the Declaration class, and the class instantiated during execution is called the instance class.

For example: Bird = new sparrow ();
Birds are declarative, And Sparrow is an instance.

The specific inspection process is as follows:

1. When a function of an object is called, the system will directly check the declared class of the object to see whether the called function is a virtual function;

2. If it is not a virtual function, it will directly execute the function. If there is a virtual keyword, that is, a virtual function, it will not immediately execute the function at this time, but will go to check the instance class of the object.

3. In this instance class, he checks whether the virtual function is re-implemented in the definition of this instance class (through the override keyword). If yes, OK, and immediately execute the re-implemented function in the instance class. If no, the system will keep searching for the parent class of the Instance class and repeat the check in the instance class, until you find the first parent class that reloads the virtual function, and then execute the function that is reloaded in the parent class.

By knowing this, you can understand the running results of the following code:

Using system;

Namespace zhisi. net
{
Class
{
Public Virtual void func () // note virtual, indicating this is a virtual function
{
Console. writeline ("func in ");
}
}

Class B: A // note that B is inherited from Class A, so Class A is the parent class, and Class B is the subclass.
{
Public override void func () // note override, indicating that the virtual function is re-implemented.
{
Console. writeline ("func in B ");
}
}

Class C: B // note that C is inherited from Class A, so B is the parent class, and C is the subclass.
{
}

Class D: A // note that B is inherited from Class A, so Class A is the parent class, and Class D is the subclass.
{
Public new void func () // note new, indicating to overwrite the Same Name class in the parent class, instead of re-Implementing
{
Console. writeline ("func in B ");
}
}

Class Program
{
Static void main ()
{
A A; // defines an object of Class A. A is the declarative class of Class.
A B; // defines an object of Class a B. A is the declarative class of Class B.
A c; // defines the object of Class A of class C. A is the declarative class of Class B.
A d; // defines the object of Class A, which is the declarative class of Class B.

A = new A (); // instantiate object A. A is an instance class of.
B = new B (); // instantiate the B object. B is the B instance class.
C = new C (); // instantiate the B object. c is the B instance class.
D = new D (); // instantiate the B object. D is the B instance class.

A. func (); // execute. FUNC: 1. first check the declarative Class A 2. check to be a virtual method 3. switch to check instance Class A, it is itself 4. execute Method 5 in instance Class. output result func in
B. func (); // execute B. FUNC: 1. first check the declarative Class A 2. check to be a virtual method 3. switch to check instance Class B, which has heavy loads. 4. execute the method in instance Class B 5. output result func in B
C. func (); // execute C. FUNC: 1. first check the declarative Class A 2. check to be a virtual method 3. switch to check instance Class C, no heavy load 4. go to check the parent class B of class C, which has 5 of the reload. execute the func method in parent class B. 5. output result func in B
D. func (); // execute D. FUNC: 1. first check the declarative Class A 2. check to be a virtual method 3. switch to check instance Class D, without overloading (this should be noted, although D has implemented func (), but does not use the override keyword, so it will not be considered as a heavy load) 4. to check the parent class A of class D, it is itself 5. execute the func method in parent class A. 5. output result func in
D D1 = new D ();
D1.func (); // execute func () in Class D and output the result func in D.
Console. Readline ();
}
}
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shamozhu/archive/2009/01/19/3835664.aspx

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.