Delphi Object Model (Part V)

zhaozj2021-02-11  162

Delphi Object Model (Part V)

Delphi is rich and powerful for object-oriented programming. In addition to traditional classes and objects, Delphi also provides features such as interface, abnormal processing, multi-threaded programming. This chapter explains the object model of Delphi. The reader should be familiar with the standard Pascal and have a certain understanding of the basic law on object-oriented programming.

(The original text of this article will decompose Delphi and Object Pascal as Delphi, may have a conceptual suspected. However, in most cases, I believe that readers can determine the specific meaning of Delphi described in the text according to context - Translator's Note .)

Indexed Properties index type properties

You can map many different properties to a way that is the same read or write, just specify an index value for each property. This index value will be passed or written to distinguish between different properties.

You can even use the array type and index types. The reader and writer will distinguish between them - the index of the array as the first parameter, then the index value.

Default Values ​​Default

The properties are also used in Stored and Default two indicators. The information here is not large with the characteristic relationship between the Delphi Object Pascal language, but the IDE of Delphi uses it as a description of the FORM. The value of the Stored indicator can be a constant of a Boolean type or a Boolean type field, or a Boolean value method that does not require any parameters directly. The value of the default indicator should be the same constant as the type of this property. Only the properties of the enumeration, shaping, and set types can have a default value. The Stored and Default indicators are only meaningful for published properties.

In order to distinguish the default array number and the default value, the default array indicator is followed by a semicolon followed by a declaration of attributes. The default value indicator is placed directly after the attribute declaration. See Chapter 5 Learn about the contents of the Default indicator.

Using Properties Using Properties

Usually, when you define a class, we set all the fields to private, then declare that many public properties are visited. However, Delphi did not expire the attribute direct access field method. But use properties, you can change the implementation of properties at a certain time, such as changes in field values ​​to increase checks. You can also use properties to enforce access levels, such as using a read-only attribute when it is not changed by a field. Example 2-11 shows several ways to declare and use properties.

Example 2-11: Declaration and use properties

Type

TCUSTOMER = Record

Name: String;

TaxidNumber: String [9];

END;

Taccount = Class

Private

FCustomer: TCUSTOMER;

FBALANCE: CURRENCY;

Fnumber: cardinal;

Procedure setBalance (NewBalance: Currency);

Published

Property Balance: Currency Read Fbalance Write setBalance;

Property Number: Cardinal Read Fnumber; // Does not change

Property Custname: String Read Fcustomer.Name;

END;

Tsavingsaccount = Class (Taccount)

Private

FINTERESTRATE: INTEGER;

Published

Property InterestRate: Integer Read FinterestRate

Write FinterestRate Default DefaultInterestRate;

END;

TLINKEDACCOUNT = Class (TOBJECT)

Private

Faccounts: array [0..1] of taccount; function getaccount (index: integer): taccount;

public

// Method for accessing arrays of two attributes: using an index or reference an array element

Property CHECKING: Taccount Index 0 Read getaccount;

Property Savings: Taccount Read Faccounts [1];

END;

TaccountList = Class

Private

FList: TLIST;

Function getAccount (INDEX: Integer): Taccount;

Procedure setAccount (INDEX: Integer; Account: taccount);

Function getCount: Integer;

protected

Property List: Tlist Read flist;

public

Property Count: Integer Read GetCount;

Property Accounts [INDEX: Integer]: Taccount Read getaccount

Write setAccount; default;

END;

Procedure Taccount.setBalance: Currency;

Begin

IF newbalance <0 THEN

Raise eoverdrawnexception.create;

Fbalance: = newbalance;

END;

Function TLINKEDACCOUNT.GETACCOUNT (INDEX: Integer): Taccount;

Begin

Result: = Faccounts [Index]

END;

Function TaccountList.getCount: Integer;

Begin

Result: = List.count

END;

Function TaccountList.getaccount (INDEX: Integer): Taccount;

Begin

Result: = List [index]

END;

Procedure TaccountList.setAccount (INDEX: Integer; Account: Taccount);

Begin

FLIST [INDEX]: = Account

END;

Class-Type Properties object Type properties

Attributes of object types need to cause extra attention. When using an object type, it is best to manage object properties by the owner of the object. That is, only one of the object is not enough, and a copy of the object property is required to retain the object attribute. This is used in a writer method. Delphi's IDE requires all published attributes to meet this requirement, and also affects unpublished attributes.

The only exception to this rule is that attributes save a reference to components on the Form. In this case, the attribute must save the object reference rather than a copy of the component.

Delphi's IDE only stores component names in .dfm to save the value of the component reference. When .dfm is loaded, the Delphi looks for the component name to recover the value referenced. If you have to save a complete component inside a component, you must implement access to the properties of the internal components.

The class of confirmation attribute is inherited from TPERSISTEN, and the class overwrites the Assign method. The writing method of the attribute is implemented by calling Assign. (TPERSISTENT, defined in the CLASSES unit, is not required, but it is indeed the simplest method - copy an object. Otherwise, you will take twice the cost to write the Assigned method in any other class.) Reading methods can provide direct access to fields. If the object has an onchange event, you prefer to set its value to make changes when it makes it. Example 2-12 shows a typical method of using an object attribute. A graphic control is defined in the example for display a bitmap in a tile in a range in its range. Attribute Bitmap stores a Tbitmap object. Example 2-12: Declaration and Use Object Types of Properties

Unit Tile;

Interface

Uses Sysutils, Classes, Controls, Graphics;

Type

// tile a bitmap

TTILE = Class (TGRAPHICCONTROL)

Private

Fbitmap: Tbitmap;

Procedure setBitmap (Newbitmap: Tbitmap);

Procedure BitmapChanged (Sender: TOBJECT);

protected

proca;

public

Constructor Create (Owner: Tcomponent); OVERRIDE;

DESTRUCTOR DESTROY; OVERRIDE;

Published

Property Align;

Property Bitmap: Tbitmap Read Fbitmap Write SetBitmap

Property Onclick;

Property OnDBLClick;

// There are also many useful methods, limited to spaces not listed. See TControl for details.

END;

IMPLEMentation

{TTILE}

// Create The Bitmap When Creating The Control.

Constructor TTILE.CREATE (Owner: tcomponent);

Begin

inherited;

Fbitmap: = tbitmap.create;

Fbitmap.onchange: = BitmapChanged;

END;

// free the bitmap when destroying the control.

Destructor TTILE.DESTROY;

Begin

Freeandnil (Fbitmap);

inherited;

END;

// when Bitmap Changes, RedRaw The Control.

Procedure TTILE.BITMAPCHANGED (Sender: TOBJECT);

Begin

INVALIDATE;

END;

// Paint the control by tiling the bitmap. If there is no

// bitmap, don't paint anything.

Procedure TTILE.PAINT;

VAR X, Y: Integer;

Begin

IF (Bitmap.width = 0) or (Bitmap.height = 0) THEN

EXIT;

Y: = 0;

While Y

Begin

X: = 0;

While X

Begin

Canvas.Draw (X, Y, Bitmap);

INC (X, Bitmap.width);

END;

INC (Y, Bitmap.height);

END;

// Set the new value by copying the Tbitmap object

Procedure TTILETBITMAP (Newbitmap: Tbitmap);

Begin

Fbitmap.Assign (newbitmap);

END;

End.

PartipartiipartiiipartivPartvpartvi More Articles

转载请注明原文地址:https://www.9cbs.com/read-5325.html

New Post(0)