Virtual Methods, dynamic methods, abstract methods, constructor methods, method overloading, and method inheritance

Source: Internet
Author: User

 

Virtual Method

The virtual method isDelphiUse the keyword virtual to define. After a method is declared as a virtual method,ProgramYou need to dynamically determine the method to be called.

Now, redefine the above method. Note that the keyword override is used to indicate method overloading.CodeAs follows:

Type

Tfigure = Class

Procedure draw; virtual;

Procedure destroy

End;

Procedure tfigure. Draw;

Begin

Writeln ('the current call is a tfigure object ');

End;

Type

Trectangle = Class (tfigure)

Procedure draw; override;

Procedure destroy

End;

Procedure trectangle. Draw;

Begin

Writeln ('currently calling the trectangle object ');

End;

Type

Tellipse = Class (tfigure)

Procedure draw; override;

Procedure destroy

End;

Procedure tellipse. Draw;

Begin

Writeln ('the current call is the tellipse object ');

End;

The following code calls the draw method of the preceding graphic object:

VaR

Figure: tfigure;

Begin

Figure: = trectangle. Create;

Figure. Draw; // call trectangle. Draw

Figure. Destroy;

Figure: = tellipse. Create;

Figure. Draw; // call tellipse. Draw

Figure. Destroy;

End;

In the above Code, when figure. Draw is called for the first time, the current object is of the trectangle type; when figure. Draw is called for the second time, the current object is of the tellipse type.

The output is as follows:

Now the trectangle object is called.

The tellipse object is called now.

Dynamic Method

Dynamic methods are implemented using the dynamic keyword. Their functions are similar to those of virtual methods. Different functions are mainly reflected in internal implementation. Unlike the object virtual method creation portal, dynamic assigns a number to the method and stores the address of the corresponding code. The dynamic method list only contains the newly added and overwritten method entries. The inherited dynamic method matches by finding the dynamic method list of Each ancestor (in reverse order of inheritance ") therefore, the dynamic method is used to process messages (including Windows messages ). In fact, the matching method of the message processing process is the same as that of the dynamic method, but the defining method is different.

The dynamic method does not have an entry to the object VMT (virtual method table), which reduces the amount of memory consumed by the object. Dynamic Method matching is slower than virtual method matching. Therefore, if a method is called frequently, it is best to define it as a virtual method.

Abstract Method

Abstract methods are implemented using the abstract keyword. If a method is defined as abstract, this method does not need to be implemented in this class. In its subclass, functions can be implemented in the overload method.

In the following code, the draw method declaration of the tfigure object is abstract, so tfigure is not required. draw method. In its subclass, subclass data is reloaded and implemented based on different requirements.

Type

Tfigure = Class

Procedure draw; virtual; abstract;

Procedure destroy

End;

Type

Trectangle = Class (tfigure)

Procedure draw; override;

Procedure destroy

End;

Procedure trectangle. Draw;

Begin

Writeln ('currently calling the trectangle object ');

End;

Type

Tellipse = Class (tfigure)

Procedure draw; override;

Procedure destroy

End;

Procedure tellipse. Draw;

Begin

Writeln ('the current call is the tellipse object ');

End;

If the abstract method of a class is not overloaded, this method cannot be called. Otherwise, internal calling of the abstract method is generated.DelphiError.

Method overload

 

As mentioned above, override (method overload) can be used to achieve polymorphism. Method overloading means that multiple methods can have the same name. You can reload a method by declaring a method with the same name as the ancestor object in the descendant object. If you want this method to do the same work as the ancestor object in the descendant object, but use different methods, you can reload this method.

The reload will fail in the following situations:

(1) There is no method to be overloaded in the ancestor class.

(2) methods to be overloaded are declared as static methods in the ancestor class.

(3) The parameter types or numbers of methods to be overloaded are different from those of the ancestor class.

Method inheritance

 

DelphiUse keywordsInheritedMutual inheritance between implementation methods.

IfInheritedIf there is no method name after the keyword, the current method inherits the method with the same name as it in the ancestor class; ifInheritedIf the keyword is followed by a method name, the current method inherits the method specified in the ancestor class.

Method inheritance is often used in constructor methods, so that subclass can implement some default settings in the ancestor class.

Constructor

 

Constructor is a special method that allocates memory for a new object and points to the new object.DelphiEach class in has a constructor to initialize a new object of the class.

The constructor keyword is used to declare the constructor. The constructor name can be selected randomly,DelphiGenerally, create is used for letter numbers. Constructor can use multiple,DelphiWe recommend that you define only one constructor for each object.

DelphiThe internal implementation process of the constructor includes:

(1) first, open up a region in the memory to store objects.

(2) initialize the settings for this region by default. Initialization, including clearing fields of the ordered type, setting fields of the pointer type and class type to nil, and clearing fields of the string type to null.

(3) execute the action specified by the user in the constructor.

(4) return a newly allocated and initialized class type instance. The type of the returned value must be the type of the class.

To create an instance of an object, you must first call the create method, and then the constructor assigns this instance to a variable. The constructor does not return any data types. In the constructor implementation, you can also overload the methods, as shown in the following example:

Type

Tshape = Class (tgraphiccontrol)

Private

Fpen: tpen;

Fbrush: tbrush;

Procedure penchanged (Sender: tobject );

Procedure brushchanged (Sender: tobject );

Public

Constructor create (owner: tcomponent); override;

Destructor destroy; override;

...

End;

M

Constructor tshape. Create (owner: tcomponent );

Begin

InheritedCreate (owner); // initializeInheritedParts

Width: = 65; // changeInheritedProperties

Height: = 65;

Fpen: = tpen. Create; // Initialize New Fields

Fpen. onchange: = penchanged;

Fbrush: = tbrush. Create;

Fbrush. onchange: = brushchanged;

End;

In the preceding example, the tshape class reloads the tgraphiccontrol constructor. Within the constructor, The tshape class first inherits the default constructor of tgraphiccontrol, implements some general settings, and then initializes its own parameters by setting some special parameters, at the same time, make yourself different from the ancestor class.


Another constructor instance

The constructor of Delphi is defined:
Constructor create;
The destructor of Delphi is defined:
Destructor destroy;
Destructor cannot be reloaded, but constructors can be reloaded.
During overload, the constructor must add "overload" to the end. For example:
Constructor create; overload;
Constructor create (I: integer); overload;
Note that only two constructors or more are called overload. If only one constructor is used, "overload;" is not required.
The default constructor create; if there is a heavy load, the default constructor will also be followed by overload, just as in the above example.
Where is the definition of the Delphi constructor outside the class? After implementation. The following example shows the definition of the constructor:

Unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;

Type
Tform1 = Class (tform)
Button1: tbutton;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;

Tmyform1 = Class (tform1) // customize a tmyform1 class
Public
Constructor create; overload; // The constructor is overloaded.
Constructor create (I: integer); overload; // reload a constructor
End;

VaR
Form1: tform1;

Implementation

{$ R *. DFM}
Constructor tmyform1.create; // The constructor is defined here.
Begin
Inherited create (NiL); // inherited indicates that the constructor of the parent class is called.
End;
Constructor tmyform1.create (I: integer );
Begin
Inherited create (NiL );
End;

Procedure tform1.button1click (Sender: tobject );
VaR
A: tmyform1;
Begin
A: = tmyform1.create (1 );
A. show;
End;

End.

 

 

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.