Delphi Object Model (Part I)

zhaozj2021-02-11  185

Delphi Object Model (Part I)

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 .)

Classes and Objects class and objects

You can imagine a special record. Similar to the record, a class describes a special type, which consists of any plurality of parts, each part being called a field. Different from the structure, a class can include functions and procedures (called method method), as well as attribute Property. A class can inherit from another class, so it inherits all fields, methods, and properties in the ancestral class.

An object (Object) is a dynamic instance of a class. Objects are always allocated from the heap, so an object reference is more like a pointer (but does not require PASCAL ^ operators). When you assign an object reference to a variable, Delphi just copies the pointer instead of copying the entire object instance. When an object is not ended in the program, you should call the Free method to explicitly release the object. Delphi did not provide an automatic garbage collection mechanism (except for the interface mentioned in the following chapter).

For a short, the term "object reference" is referred to as "object", but the more accurate expression of the object should be a memory, and the Delphi stores the value of all the fields of the object. The only way to use an object in Delphi is to use object references. An object reference typically exists in the form of a variable, but there is also a function of a function or attribute return value.

The class is also an independent entity (similar to Java, but different from C ). In Delphi, the class behaves as a read-only table in memory, and there is a pointer to the false method of this class and many other information in the table. A class reference is a pointer to the table. The most common usage of class references is to create an object or to test the type of object reference, or you can use in many other occasions. For example, the transfer class references to a routine or returns a class reference from a function. Types of class references are called Metaclass.

Example 2-1 shows a few classes of statements. Class declaration begins with keyword class. The specification of the field contains the field (the "Field, the method, the property, etc., ends with the keyword END. The declaration of each method is similar to the Forword leader declaration, you must implement it in the same unit (except the Abstract method, the content of the abstract method will be mentioned later).

Type

Taccount = Class

Private

Fcustomer: String; // Name of Customer

Fnumber: cardinal; // Account Number

Fbalance: currency; // current account balance

END;

Tsavingsaccount = Class (Taccount)

Private

FINTERESTRATE: Integer; // Annual Percentage Rate, Scaled By 1000

END;

TcheckingAccount = Class (taccount) Private

FRETURNCHECKS: BOOLEAN;

END;

TcertificateOfDeposit = Class (tsavingsaccount)

Private

Fterm: cardinal; // CD Maturation Term, in Days

END;

VAR

CD1, CD2: TACCOUNT;

Begin

CD1: = TcertificateOfDeposit.create;

CD2: = TcertificateOfDeposit.create;

...

Figure 2-1 depicts the objects in Example 2-1 and the storage structure in memory. Variables and related objects are stored in readable and writable memory. The class is placed in the read-only memory and is placed with the program code.

Figure 2.1

Delphi's object model is similar to other object-oriented languages, such as C and Java. Table 2-1 shows a brief comparison of Delphi and several other popular programming languages.

Table 2-1: Delphi Versus The World

Language characteristics

Delphi

Java

C

Visual Basic

inherit

Bamboo

Bamboo

Bamboo

Multiple inheritance

Bamboo

interface

Bamboo

Bamboo

[1]

Bamboo

Single

Bamboo

Bamboo

Metacity

Bamboo

Bamboo

Class (static) field

Bamboo

Bamboo

Virtual method

Bamboo

Bamboo

Bamboo

Abstract (pure) virtual method

Bamboo

Bamboo

Bamboo

Class (static) method

Bamboo

Bamboo

Bamboo

Dynamic method

Bamboo

Garbage collection

[2]

Bamboo

[2]

Variable type

Bamboo

Bamboo

OLE automation

Bamboo

Bamboo

Static type verification

Bamboo

Bamboo

Bamboo

Abnormal processing

Bamboo

Bamboo

Bamboo

Bamboo

Function (process) overload

Bamboo

Bamboo

Bamboo

Operator overload

Bamboo

Non-class function

Bamboo

Bamboo

Bamboo

Non-object variable

Bamboo

Bamboo

Bamboo

Attributes

Bamboo

Bamboo

RTTI (running type information)

Bamboo

Bamboo

[3]

Generic Type (Template)

Bamboo

Embedded thread support

Bamboo

Bamboo

Message delivery

Bamboo

Embedded compilation

Bamboo

[4]

Single line function

Bamboo

We will discuss these language characteristics in detail in the following sections.

Class (Class)

The category declaration describes the fields (Field), method, and properties, and the property (Property). You can declare a class at the unit's interface or the Implementation section, but the method (Method) - similar to the function or process - must have to define in the Implementation section. At the same time, you must implement this method within the same unit of this class.

Categories can be declared into one or more parts, allowing each part to have different access levels (can be private private, protected Protected, published public, published published, and automatic Automated, etc.). The content of the access level will be discussed later. You can even arrange any of the declarations, and allow the same access level to repeat.

In each part of the declaration, you can define any more fields, follow the declaration of methods and properties. The declaration of methods and attributes can be mixed, but all fields in the same part must be declared before the method. Unlike Java and C , Delphi cannot nescery other types of statements in class declarations.

Class only has a single base class, inheriting all fields, properties, and methods from the base class. If you don't clearly specify the base class, Delphi automatically uses TOBJECT as the base class. Category can achieve any more interface. Thus, Delphi's object model is similar to Java, that is, a class can expand a simple class and implement multiple interfaces. Tip: There is a convention in Delphi, and the type name is usually headed by letters T, such as Tobject. However, this is just an agreement rather than a grammar rule. Also, the IDE is usually named a class in one t.

Class references are a reference to a particular class. Class references are not a class object (so in Jave and SmallTalk), but it can be used to create new objects, call class methods, and types of tests or test objects. The class reference is implemented in a pointer, and this pointer points to a table related to this type of information, including the virtual method table (VMT) of the class. (See the Third Chapter "" What is it in the VMT "related content.)

The most common usage of class references is to call the constructor constructor of the class to create an object instance. You can also use class reference to detect the type of object (use IS operator). Normally, class reference is a class name, but it can be a metaClass type variable, or the return value of the function and attribute.

Example 2-2: Declare a class and the classification

Type

TCOMPLEXCLASS = Class of Tcomplex; // Metclass type

Tcomplex = Class (TPERSIStent)

Private

FREAL, FIMAGINARY: DOUBLE;

public

Constructor Create (Re: double = 0.0); overload;

Constructor Create (RE, IM: DOUBLE); OVERLOAD;

DESTRUCTOR DESTROY; OVERRIDE;

Procedure assign (Source: TPERSIStent); OVERRIDE;

Function asstring: string;

Published

Property REAL: Double Read FREAL WRIAL

Property

END;

Partii Partiii

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

New Post(0)