Delphi's Virtual,dynamic,abstract

Source: Internet
Author: User

Delphi's Virtual,Dynamic,Abstractobject can be defined statically (static), virtual (Virtual), Dynamic (Dynamic) or message processing (message). Take a look at the following example: Tfoo=class procedureiamastatic;procedureiamavirtual;Virtual; procedureiamadynamic;Dynamic; procedureIamamessage (varM:tmessage);messageWm_somemessage;End; 1The static method Iamastatic is a static method, which is the default type of the method, as it is called for normal procedures and functions. The compiler knows the address of these methods, so when a static method is called it can statically link the run information into the executable file. Static methods perform the fastest, but they cannot be overwritten to support polymorphism. 2the virtual method Iamavirtual is a virtual method. Virtual methods and static methods are called the same way. Because the virtual method can be overwritten, the compiler does not know its address when invoking a specified virtual method in code. Therefore, the compiler finds the address of the function at run time by establishing a virtual method table (VMT). All virtual methods are dispatched at run time by VMT, and the VMT table of an object, in addition to its own defined virtual method, has all the virtual methods of its ancestors, so the virtual method uses more memory than the dynamic method, but it executes faster. 3The dynamic method Iamadynamic is a dynamic method, and the dynamic method is basically similar to the virtual method, except that their scheduling system is different. The compiler assigns a unique number to each dynamic method, and constructs a dynamic method table (DMT) with the address of this number and the dynamic method. Unlike the VMT table, there is only a dynamic method declared in the DMT table, and this method requires an ancestor's DMT table to access its remaining dynamic methods. Because of this, the dynamic method uses less memory than the virtual method, but is slower to execute because it is possible to find a dynamic method in the DMT of the ancestor object. 4The message-handling method, Iamamessage, is a message-handling method, and the value following the keyword message indicates the message to which the method responds. Use the message processing method to respond to Windows messages so that you do not have to call it directly. 5The override of the method in Object Pascal overrides a method used to implement the concept of the polymorphism of OOP. Overrides enable a method to behave differently between different derived classes. The method that can be overridden in Object Pascal is a method that is identified as virtual or dynamic at the time of declaration. To override a method, replace virtual or dynamic with override in the declaration of the derived class. For example, you can override the Iamavirtual and Iamadynamic methods with the following code: Tfoochild=class(Tfoo)procedureiamavirtual;Override; procedureiamadynamic;Override; procedureIamamessage (varM:tmessage);messageWm_somemessage;Endafter using the override keyword, the compiler replaces the original method in VMT with a new method. If you replace with virtual or dynamicOverridere-declaring iamavirtual and iamadynamic will be to create new methods instead of overwriting ancestors ' methods. Similarly, in a derived class, if an attempt is made to overwrite a static method, the method in the new object completely replaces the method with the same name in the Ancestor class. 6the overloads of the. method are like normal procedures and functions, and the method also supports overloading, so that there are many methods with the same name in a class with different parameter tables, the overloaded method must be identified with the overload indicator, and the first method can not be used overload. The following code shows a method that has three overloads in a class:typeTsomeclass=class procedureAmethod (I:integer);Overload; procedureAmethod (i:string);Overload; procedureAmethod (iouble);Overload; End; 7. Reintroduce method names Sometimes, you need to add a method to a derived class that has the same name as a method in the Ancestor class. In this case, it is not necessary to overwrite this method, as long as the method is re-declared in the derived class. At compile time, however, the compiler will issue a warning telling you that the method of the derived class will hide the method with the same name as the ancestor class. To work around this problem, you can use it in a derived classReintroduceindicator, the following code demonstrates the correct use of the reintroduce indicator:typeTsomebase=class procedureCooper;End; Tsomeclass=class(tsomebase)procedureCooper;Reintroduce; End; 8. Self in all object methods there is an implied variable called self,self is a pointer to the class instance that is used to invoke the method. Self is passed to the method by the compiler as an implicit parameter. Virtual,Dynamic: Virtual and dynamic methods are semantically equivalent, but the former is time-optimal and the latter is spaceAbstractabstract methods are virtual or dynamic methods that are declared in a class but are not implemented. The implementation of the abstract method is deferred to the descendant class. Declaring an abstract method must use abstract after the indicator word virtual or dynamic. For example:proceduredosomething;Virtual;Abstract; Cannot invoke an abstract method that does not have an implementation or defines a declaration. OverrideOverloaded to achieve polymorphism. Implement or overwrite virtual,Dynamic,Abstractthe statementOverloadThen define a function with the same name but with different parameters. (Automatically selected according to the parameters when called) and classObjectIt doesn't matter .stdcallUse the standard Win32 parameter delivery technique. Other such as Register,cdecl,Pascal messageMessage Processing (ReintroduceRepresents a method that is re-declared; the name of the method is the same as a method name in an ancestor class)

Delphi's Virtual,dynamic,abstract

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.