A method is a procedure and function that belongs to a given object, and the method reflects the behavior of the object rather than the data, two important methods of the object mentioned in the previous article: the construction method and the destructor method.
To enable the object to perform various functions, you can customize the method in the object
Create a method with two steps, first declare this method in the declaration of the object type. Then define the method in code. The following code demonstrates the steps to declare and define a method
Type tboogienights = Class Dance:boolean; Procedure Dothehustle; End;procedure tboogienights.dothehustle;begin dance:= true;end;
Note: When defining a method body, you must use the full name, as you would when defining a method Dothehustle. It is also important to note that in this method, the dance domain of the object can be accessed directly
I. Types of methods
An object's methods can be defined as static (static), virtual, dynamic, or message processing (messages). Take a look at 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, the static method is the default type of the method, which is called as usual procedure and function. The compiler knows the address of these methods, so when a static method is called it can make information statically linked into the executable file . static methods perform the fastest, but they cannot be overwritten to support polymorphism
2) 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 more quickly
3) 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 one dynamic method declared in the DMT table, and this method requires an ancestor's DMT table to access its other 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
4) Message Handling methods
Iamamessage is a message processing method, and the value following the keyword message indicates the message to which this method responds. Use the message processing method to respond to Windows messages so that you do not have to call it directly .
Ii. coverage of methods
In Delphi covers a method used to implement the abortion concept of OOP. overrides enable a method to behave differently between different derived classes . The method that can be overridden in Delphi 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 a derived class, for example, overwrite the Iamavirtual and Iamadynamic methods with the following code:
Tfoochild = Class (Tfoo) procedure iamavirtual; override; Procedure iamadynamic; Override; Procedure Iamamessage (var m:tmessage); Message wm_somemessage;end;
with the override keyword, the compiler replaces the original method in VMT with a new method .
Note: If you re-declare iamavirtual and iamadynamic with virtual or dynamic instead of override, you will create a new method instead of overwriting the 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 .
Iii. Overloading of methods
Just like normal procedures and functions, methods also support overloading, so that there are many methods of the same name in a class with different parameter tables, the overloaded method must be identified with the overload indicator, you can not use the first method with overload, the following code shows a class with three overloaded methods:
Type Tsomeclass = class procedure Amethod (I:integer); overload; Procedure Amethod (s:string); overload; Procedure Amethod (d:double); overload; End
Iv. re-introduction of the method name
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. But at compile time, the compiler will issue a warning 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 the reintroduce indicator in a derived class, and the following code demonstrates the correct use of the reintroduce indicator
Type tsomebase = class procedure Cooper; End; Tsomeclass = Class (Tsomebase) procedure Cooper; reintroduce; End
V. Self
In all object methods, there is an implied variable called self,self, which is a pointer to the class instance that is used to invoke the method.
Self is passed by the compiler as an implicit parameter to the method
Delphi Object-oriented methods