Understanding Delphi classes (11)-in-depth methods in classes [1]-virtual and dynamic methods

Source: Internet
Author: User
Methods come to the class, and the previous features are basically in;
Classes generally exist in an inheritance chain, so there are some new concepts, such as inheritance and overwrite;
There are also many new names, such as static methods, virtual methods, dynamic methods, abstract methods, class methods, and message methods.

Start with the virtual and dynamic methods.

// The following Classes define two virtual methods, two dynamic methods (dynamic) tmyclass = Class procedure proc1 (X, Y: Real), and virtual; function fun1 (X, Y: Real): real; virtual; Procedure proc2 (X, Y: Real); dynamic; function fun2 (X, Y: Real): real; dynamic; end;
  
   
 // Defined as a virtual or dynamic method, which means that the child classes will be overwritten (override), that is, override tbass = Class procedure proc (X, Y: Real ); virtual; function fun (X, Y: Real): real; dynamic; end; tchild = Class (tbass) Procedure proc (X, Y: Real); override; function fun (X, Y: Real): real; override; end; {This mechanism forms polymorphism}
  
   
 // What is the difference between the virtual method and the dynamic method?
Each class contains two tables: virtual table (VMT) and dynamic table (DMT );
The VMT table contains the virtual methods of this class and all its parent classes-it is generally a relatively large table;
DMT tables only contain the dynamic methods of this class-if you want to call the dynamic methods of its upper class, you can only find them step by step;
Therefore, the speed of using virtual methods has an advantage, and the use of dynamic methods will save memory;

At the early stage of Delphi, there was only virtual instead of dynamic. Later, with the increasing size of VCL, dynamic was available. For example, the event methods of classes were generally defined in the early stage. In order to save space, event methods are basically defined as dynamic in VCL;

In this case, virtual and dynamic are not much different. One focuses on speed and saves space. They can be replaced by one another!

In addition, because they are not very different, and they are virtual first, people are also used to calling "virtual methods" and "dynamic methods" as "virtual methods ".

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.