Delphi Method Type

Source: Internet
Author: User
Object methods can be defined as static, virtual, dynamic, or message processing ). See the following
Example:

Tfoo = Class
Procedure iamastatic;
Procedure iamavirtual; virtual;
Procedure iamadynamic; dynamic;
Procedure iamamessage (var m: tmessage); message wm_somemessage;
End;

1. Static Method
Iamastatic is a static method, which is the default type of the method.
. The compiler knows the address of these methods, so when calling a static method, it can statically link the running information to the executable file.
Static Methods run at the fastest speed, but they cannot be overwritten to support polymorphism.

2. Virtual Methods
Iamavirtual is a virtual method. The call Method for virtual and static methods is the same. Because the virtual method can be overwritten
When the code calls a specified virtual method, the compiler does not know its address. Therefore, the compiler creates a virtual method table (VMT)
To find the function address at runtime. All virtual methods are scheduled through VMT at run time. An object's VMT table
In addition to its own virtual methods, there are also all the virtual methods of its ancestor. Therefore, virtual methods require more memory than dynamic methods.
But it runs faster.

3. Dynamic Methods
Iamadynamic is a dynamic method. dynamic methods are similar to virtual methods, but their scheduling systems are different. Compile
Specify a unique number for each dynamic method. Use the address of this number and the dynamic method to construct a dynamic method table.
(DMT ). Unlike the VMT table, the DMT table only has the dynamic method it declares, and this method needs to be accessed by the DMT table of the ancestor.
Other dynamic methods. Because of this, the dynamic method uses less memory than the virtual method, but the execution is slow because it is possible
Search for dynamic methods in DMT of the ancestor object.

4. Message Processing Method
Iamessage is a message processing method. The value after the keyword message specifies the message to be responded to by this method.
Use message processing methods to respond to Windows messages, so that you do not need to call them directly.

5. Method coverage
Override a method in Object Pascal to implement the concept of OOP polymorphism. Overwrite to make a method between different Derived classes
Shows different behaviors. The method that can be overwritten in Object Pascal is identified as virtual or dynamic during declaration. Is
Overwrites a method, replacing virtual or dynamic with override in the declaration of the derived class. For example, you can use the following code to overwrite
Iamavirtual and iamadynamic methods:

Tfoochild = Class (tfoo)
Procedure iamavirtual; override;
Procedure iamadynamic; override;
Procedure iamamessage (var m: tmessage); message wm_somemessage;
End;

After the override keyword is used, the compiler replaces the original VMT method with a new method. Replace with virtual or dynamic
Override re-declaring iamavirtual and iamadynamic will create a new method instead of overwriting the method of the ancestor. Similarly,
In a derived class, if an attempt is made to overwrite a static method, the method in the new object is completely replaced by the same name method in the ancestor class.

6. Method Overloading
Like common procedures and functions, methods also support overloading, so that many methods with the same name in a class have different parameter tables,
Methods that can be reloaded must be identified by the overload indicator. You may not use overload for the first method. The following code demonstrates
There are three overload methods in each class:

Type
Tsomeclass = Class
Procedure amethod (I: integer); overload;
Procedure amethod (I: string); overload;
Procedure amethod (I: Double); overload;
End;

7. re-introduce method name
Sometimes, you need to add a method to the derived class, and the method name is the same as a method name in the ancestor class. In
In this case, there is no need to overwrite this method, as long as it is re-declared in the derived class. However, during compilation, the compiler will issue
Generates a warning that the method of the derived class will hide the method of the same name of the ancestor class. To solve this problem, you can use
Reintroduce indicator. The following Code demonstrates the correct usage of the reintroduce indicator:
Type
Tsomebase = Class
Procedure Cooper;
End;
Tsomeclass = Class (tsomebase)
Procedure Cooper; reintroduce;
End;

8. Self
In all object methods, there is an implicit variable called self, which is a pointer to a class instance used to call methods. Self
The compiler is passed to the method as an implicit parameter.

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.