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 to overwrite the virtual function, and then execute the rewritten function in the parent class.

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

[C-sharp]
01. using System;
02. namespace Smz. Test
03 .{
04. class
05 .{
06. public virtual void Func () // note virtual, indicating this is a virtual function
07 .{
08. Console. WriteLine ("Func In ");
09 .}
10 .}
11. class B: A // note that B is inherited from class A, so A is the parent class and B is the subclass.
12 .{
13. public override void Func () // note override, indicating that the virtual function is re-implemented.
14 .{
15. Console. WriteLine ("Func In B ");
16 .}
17 .}
18. class C: B // note that C is inherited from class A, so B is the parent class, and C is the subclass.
19 .{
20 .}
21. class D: A // note that D is inherited from class A, so A is the parent class, and D is the subclass.
22 .{
23. public new void Func () // note new, which indicates to overwrite the class with the same name in the parent class, instead of re-Implementing
24 .{
25. Console. WriteLine ("Func In D ");
26 .}
27 .}
28. class program
29 .{
30. static void Main ()
31 .{
32. A a; // defines an object of Class a. A is the declarative class of Class.
33. A B; // defines an object of Class A B. A is the declarative class of Class B.
34. A c; // defines the object of Class A of class c. A is the declarative class of class c.
35. A d; // defines the object of Class A of class d. A is the declarative class of class d.
36. a = new A (); // instantiate object a. A is an instance class of.
37. B = new B (); // instantiate the B object. B is the B instance class.
38. c = new C (); // instantiate the c object. C is the c instance class.
39. d = new D (); // instantiate d object. D is the instance class www.2cto.com of d.
40. 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
41. 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 been rewritten 4. execute the method in instance Class B 5. output result Func In B
42. 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 without rewriting 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
43. 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 rewriting (this should be noted that, although there are implementations of Func () in D, but the override keyword is not used, so it is not considered as a rewrite) 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
44. D d1 = new D ();
45. d1.Func (); // execute Func () In Class D and output the result Func In D.
46. Console. ReadLine ();
47 .}
48 .}
49 .}
 

From the column in shamozhu

Related Article

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.