Virtual function in virtual C #

Virtual functions virtual brief

In one declared virtual base class and member function is called to redefine the virtual function in one or more of the derived class.

The effect is to achieve virtual function polymorphism (Polymorphism), polymorphism interface and implementation separation.

C # as a fully object-oriented language, all functions are not virtual by default, but can be declared virtual keyword in the base class, you can override this by keyword override its function in a derived class. virtual function after rewriting is still virtual function. Since the virtual member function only instance of the class in a meaningful, so the member fields and static functions can not be declared as virtual, it can not be used together with the override and abstract.

Virtual function difference with the general function

General function at compile time statically compiled into the executable file, the relative address is not changed during program execution, which is written dead! The virtual function during the compilation is not statically compiled, its relative address is uncertain, it will dynamically determine the function to be called according to the object instance is running time, which defines the class declaration called the class declaration, the implementation examples of the class called instances of the class.

Virtual functions virtual

1, when a function is called an object, the system directly to check the object class definition statement, i.e., the class declaration, a function called to see whether the virtual function.

2, if not a virtual function, then it directly execute the function. And if there is virtual keyword, which is a virtual function, so this time it will not execute the function immediately, but to turn to the inspection object instance of the class.

3, in this instance of the class, he checks to see if reimplement the virtual function, if there is, then immediately execute the instance of this class reimplemented functions (via the override keyword) define the instance of the class. If not, the system will be kept up to find the parent class instance of the class, and parent class repeat the inspection at the instance of class, until the first override the virtual function until the parent class, and then execute the parent class in function after the overload.

Example:

operation result:

Common interview questions:

 1 class Program
 2 {
 3     class A
 4     {
 5         public A()
 6         {
 7             PrintFields();
 8         }
 9         public virtual void PrintFields() { }
10     }
11 
12     class B : A
13     {
14         int x = 1;
15         int y;
16 
17         public B()
18         {
19             y = -1;
20         }
21 
22         public override void PrintFields()
23         {
24             Console.WriteLine("x={0},y={1}", x, y);
25         }
26     }
27 
28     static void Main(string[] args)
29     {
30         new B();             
31     }
32 }

Q: When creating an instance of B using new B (), to produce what output?

A: x = 1, y = 0

Analysis: Create instance of B, no first reference implicit call parent class constructor A executes the PrintFields () method is a virtual method to check, to check the instances of the class B turn, has overloaded methods, the method performs overloaded output x = 1, y = 0

Note: The first calls the parent class constructor when creating a subclass of the object before it calls itself a subclass constructor, if not specify which one you want to call the parent class constructor, the system will implicitly call no parent class arg constructor

End!

Guess you like

Origin www.cnblogs.com/gygg/p/11556005.html