Method Type
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
A static method 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 Method
Iamavirtual
Is a virtual method. The call Method for virtual and static methods is the same. Because the virtual method can be overwritten
CodeThe compiler does not know its address when calling a specified virtual method. 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 Method
Iamadynamic
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 following the keyword message indicates 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. Object
The method that Pascal can override 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 (iouble );
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.
Virtual,
Dynamic: Virtual and dynamic methods are equivalent in semantics, but the former is the best in time and the latter is space.
Abstract
Abstract methods are virtual or dynamic methods declared in the class but not implemented.
The implementation of abstract methods is extended to descendant classes.
To declare an abstract method, you must use abstract after the indicator virtual or dynamic.
For example:
Procedure dosomething; virtual;
Abstract;
You cannot call an abstract method without implementation or definition.
Override overload to achieve polymorphism. Implement or override virtual,
Dynamic, abstract statement
Overload defines a function with the same name but different parameters (automatically selected according to the parameter when called) and
No relationship
Stdcall uses the standard Win32 parameter transfer technology. Others include register, cdecl, and Pascal.
Message
Message Processing
(Reintroduce indicates a re-declaration method. It is used when the method name is the same as a method name in the ancestor class)