If an instance method declaration is preceded with the virtual keyword, then this method is a virtual method.
The biggest difference between virtual and non-virtual methods is that the implementation of a virtual method can be replaced by a derived class, which is implemented by means of rewriting (later)
Characteristics of Virtual methods:
Static,abstract not allowed before virtual method, or override modifier
A virtual method cannot be private, so the private modifier cannot be used
Execution of virtual methods:
We know that the general function is statically compiled into the execution file at compile time, and its relative address is not changed during program operation.
While the virtual function is not statically compiled during compilation, its relative address is indeterminate, it will dynamically judge the function to be called according to the runtime object instance.
The class defined in the declaration is called the Declaration class, and the class that is instantiated at execution time is called an instance class.
such as: A A =new B (); Where a is a declaration class and B is an instance class.
1. When invoking a function of an object, the system will go directly to the object declaration definition of the class, that is, declare the class, see if the function called is a virtual function;
2. If it is not a virtual function, then it executes the function directly. And if it is a virtual function, then it does not immediately execute the function, but instead begins to examine the object's instance class.
3. In this instance class, he checks to see if there is a way to implement the virtual function or to re-implement the virtual function (via the override keyword) in the definition of the instance class.
If there is, it will not be looked up, but immediately execute the method of the virtual function implemented in the instance class. And if not, the system will keep looking up the parent class of the instance class,
and repeat the check in the instance class for the parent class until you find the first parent class that overloads the virtual function, and then execute the overloaded function in that parent class.
Example 1:
Class A {public virtual void Sum () { Console.WriteLine ("I am A class,i am Virtual Sum ()."); } Class Program { static void Main (string[] args) { a a=new a (); Defines an object of this class A. This is a declaration class, instantiates a object, a is an instance class A.sum (); Console.read (); } }
Execute a. Sum:
1. First check the declaration Class A 2. Check to be sum is virtual Method 3. Go to check instance Class A, the result is the problem itself
4. Perform the method of implementing sum in instance Class A 5. Output I am A class,i am virtual sum ().
Example 2:
Class A {public virtual void Sum () { Console.WriteLine ("I am A class,i am Virtual Sum ()."); } class B:a {public override void Sum ()//re-implements the virtual function { Console.WriteLine ("I am B class,i am override Sum ()."); } Class program { static void Main (string[] args) { A a=new B (); Define an object of this class A. This is a declaration class, instantiate a object, B is an instance of a class a.sum (); Console.read (); } }
Execute a. Sum:
1. First check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class B, there are overridden methods 4. Execute method in instance Class B 5. Output I am b class,i am override sum ().
Example 3:
Class A {public virtual void Sum () { Console.WriteLine ("I am A class,i am Virtual Sum ()."); } class B:a {public override void Sum ()//re-implements the virtual function { Console.WriteLine ("I am B class,i am override Sum ()."); } Class C:b { } class program { static void Main (string[] args) { A a=new C ();// Define an object of this class A. This is a declaration class, instantiate a object, C is an instance of a class a.sum (); Console.read (); } }
Execute a. Sum:
1. Check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class C, no Override Method 4. Go to check Class C for the parent class B, there are overridden methods
5. Execute the sum method in the parent class B 6. Output I am B class,i am override sum ().
Example 4:
Class A {public virtual void Sum () { Console.WriteLine ("I am A class,i am Virtual Sum ()."); } class B:a {public new void Sum ()//overrides the function of the same name in the parent class, instead of re-implementing { Console.WriteLine ("I am B class,i am New Sum ()."); } Class program { static void Main (string[] args) { A a=new B (); A.sum (); Console.read (); } }
Execute a. Sum:
1. Check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class B, no rewrite (this place to note, although B has implemented sum (), but does not use the Override keyword, so it is not considered to be rewritten) 4. Go to check the parent class A of Class B for itself 5. Executes the sum method in parent Class A 6. Output I am A class,i am virtual sum ().
So what if, in Example 4, the declaration is class B?
Class A {public virtual void Sum () { Console.WriteLine ("I am A class,i am Virtual Sum ()."); } class B:a {public new void Sum ()//overrides the function of the same name in the parent class, instead of re-implementing { Console.WriteLine ("I am B class,i am New Sum ()."); } Class program { static void Main (string[] args) { b b=new b (); B.sum (); Console.read (); } }
Executes sum () in class B and outputs I am B class,i am new sum ().
Can I use abstract functions to override virtual functions in a base class?
The answer is yes.
Class A {public virtual void Printfriends () { Console.WriteLine ("A.printfriends ()"); } } Abstract class B:a {public abstract override void Printfriends (); Use the override modifier to indicate that the abstraction overrides the implementation of the function in the base class } abstract class c:a {public abstract new void Printfriends (); //Using the new modifier modifier explicitly declaration, which means that the implementation of the function in the base class is hidden }
Can a sealed class have virtual functions?
Yes, virtual functions in the base class are implicitly converted to non-virtual functions, but the sealed class itself cannot add new virtual functions
Class A {public virtual void fun () { Console.WriteLine ("I am A."); } } Sealed class program:a {public override void Fun () { Console.WriteLine ("I am B."); } static void Main (string[] args) {program p = new program (); P.fun (); Console.read (); } }