Do you remember virtual? Let's restudying it down.

Source: Internet
Author: User

Work for a long time, but found that the usual mechanical development, it seems that very little use of OOP, now to the temperature.

First of all. 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:

 classA { Public Virtual voidSum () {Console.WriteLine ("I am A class,i am virtual sum ()."); }    }    classProgram {Static voidMain (string[] args) {A A=NewA ();//defines an object of this class A. This is a declaration class that instantiates a object, a is an instance class of aa.sum ();        Console.read (); }    }
View Code

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:

classA { Public Virtual voidSum () {Console.WriteLine ("I am A class,i am virtual sum ()."); }    }    classb:a { Public Override voidSum ()//virtual functions are re-implemented{Console.WriteLine ("I am B class,i am override sum ()."); }      }    classProgram {Static voidMain (string[] args) {A A=NewB ();//defines an object of this class A. This is a declaration class, instantiates a object, B is an instance class of aa.sum ();        Console.read (); }    }
View Code

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:

classA { Public Virtual voidSum () {Console.WriteLine ("I am A class,i am virtual sum ()."); }    }    classb:a { Public Override voidSum ()//virtual functions are re-implemented{Console.WriteLine ("I am B class,i am override sum ()."); }      }    classc:b {}classProgram {Static voidMain (string[] args) {A A=NewC ();//defines an object of this class A. This is a declaration class, instantiates a object, C is an instance class of aa.sum ();        Console.read (); }    }
View Code

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:

classA { Public Virtual voidSum () {Console.WriteLine ("I am A class,i am virtual sum ()."); }    }    classb:a { Public New voidSum ()//overrides a function of the same name in the parent class, not a re-implementation{Console.WriteLine ("I am B class,i am new sum ()."); }      }    classProgram {Static voidMain (string[] args) {A A=NewB ();             A.sum ();        Console.read (); }    }
View Code

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?

classA { Public Virtual voidSum () {Console.WriteLine ("I am A class,i am virtual sum ()."); }    }    classb:a { Public New voidSum ()//overrides a function of the same name in the parent class, not a re-implementation{Console.WriteLine ("I am B class,i am new sum ()."); }      }    classProgram {Static voidMain (string[] args) {b b=NewB ();             B.sum ();        Console.read (); }    }
View Code

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.

classA { Public Virtual voidPrintfriends () {Console.WriteLine ("a.printfriends ()"); }      }    Abstract classb:a { Public Abstract Override voidPrintfriends ();//use the override modifier to indicate that the abstraction overrides the implementation of the function in the base class    }    Abstract classc:a { Public Abstract New voidPrintfriends ();//The modifier explicitly declaration, using the new modifier, means that the implementation of the function in the base class is hidden}
View Code

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

classA { Public Virtual voidFun () {Console.WriteLine ("I am A."); }    }    Sealed classprogram:a { Public Override voidFun () {Console.WriteLine ("I am B."); }        Static voidMain (string[] args) {Program P=NewProgram ();            P.fun ();        Console.read (); }    }
View Code

The above content is from: http://www.cnblogs.com/jiajiayuan/archive/2011/09/14/2176015.html

We support the genuine HA

Do you remember virtual? Let's restudying it down.

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.