Early binding and late binding

Source: Internet
Author: User

Early bind early binding:

The future has been determined during compilation.ProgramWhich method of the base class or derived class is run.

CompileCodeTimeThe method defined in the reference type is determined based on the reference type.. That is, the base class method.

This operation is highly efficient.

Late binding late binding:

The method in the base class or derived class can be determined only when running.

When runningCall the corresponding method based on the actual type instead of the reference type.. It depends on what object we have new.

 

For late binding, the virtual and override keywords are introduced in C. by default, the C # method is a non-virtual method. If a method is declared as a virtual method, any class that inherits the method can implement its own version.

To call a method a virtual method, you need to use the virtual keyword before the method. Then, the derived class can use the override keyword to override the basic class virtual method, or use the new keyword to override the basic class virtual method.

If both new and override are specified, the compiler will execute the new keyword..

Example:

 Public   Class Super { Public  Virtual   Void Show () {httpcontext. Current. response. Write (" Super <br/> ");}} Public   Class Sonoverride: Super { Public   Override   Void Show () {httpcontext. Current. response. Write (" Son override <br/> ");}} Public   Class Sonnew: Super { Public  New   Void Show () {httpcontext. Current. response. Write (" Son new <br/> ");}}
PublicPartialClassMytest_default: system. Web. UI. Page {Protected VoidPage_load (ObjectSender, eventargs e) {super [] arrs =NewSuper [3]; arrs [0] =NewSuper (); arrs [1] =NewSonoverride (); arrs [2] =NewSonnew ();For(IntI = 0; I <arrs. length; I ++) {arrs [I]. Show ();}}}
 
Result:
Superson overridesuper

Let's look at the following example to understand the functions of override and new:

Public ClassA {Public Virtual VoidShow () {httpcontext. Current. response. Write ("A show <br/>");}}Public ClassB: {Public VoidShows () {httpcontext. Current. response. Write ("B Shows <br/>");}}Public ClassC: B {}

However, if the Class A is modified as follows:

Public ClassA {Public Virtual VoidShow () {httpcontext. Current. response. Write ("A show <br/>");}Public Virtual VoidShows () {httpcontext. Current. response. Write ("A shows <br/>");}}

In this case, run the following code. What is the result?

 
Protected VoidPage_load (ObjectSender, eventargs e) {A c =NewC (); C. Shows ();}

Note that instance a also has the shows method. If you want to execute a. Shows (), what will happen?

 
Protected VoidPage_load (ObjectSender, eventargs e) {A =NewB (); A. Shows ();}

Result:

A shows

In this case, the shows () method in Class B uses the New Keyword by default. What should I do if I want to use the shows method customized by Class B in the future?

The override keyword. Class B is used as follows:

Public ClassB: {Public Override VoidShows () {httpcontext. Current. response. Write ("B Shows <br/>");}}

Run the code again. The result is:

B shows
 
 
 
Conclusion:
Override ensures that all derived classes that inherit Class B use the version of the derived class defined in Class B.
To use a method in Class A, add the New Keyword before the method signed in Class B and.

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.