Custom Customs in Delphi (1)

xiaoxiao2021-03-30  204

Custom Customs in Delphi (1)

Class statement

There are two ways to declare

I. Customize the parent class DELPHI built category

Type class name = Class (parent class)

Member expression

END;

(1) Reserved word class Description This class is a DEPHI built-in, which will inherit the properties and methods of the parent class.

(2) "Parent Class" default is Delphi's ancestors TOBJECT

Second, a fully customized class

Type class name = Object (parent class)

Member expression

END;

(1) Reserved Word Object Description This class is a fully customized class

(2) Self-class must also be a completely active class

Note: Class-customized classes must follow the specific constructor and destructual methods of TOBJECT built in Delphi, and use the Object Custom Class Note (memory management mode), so use the first method to generate The Create constructor must be called when the object is

The second is not needed, directly generates an object, which will be the same as the general variable, and the recorded variable, used

Immediately allocate memory, automatically release memory using it

Customize the construction and sectations of Delphi

Constructor

Constructor Create (Aowner: Tcomponent); OVERRIDE;

(1) The parameter information can be changed according to the actual situation (parameter class, type, order, default)

(2) To inherit the constructor of the parent class, add inherited create;

Destructor

DESTRUCTOR DESTROY; OVERRIDE;

(1) If other classes are added to the custom class, they must remember to release these objects.

(2) To inherit the parent class

(3) It is worth noting that the variable of the object in DESTROY, that is, the object's reference, the object entity is not truly deleted, and there is no memory occupied by the object, and if there is no CREATE, the object reference The value is NIL, and the destroy is called, it will generate an error.

With Free solve the above problem (actually free the memory; Free calls in the case of an object is not created nor error)

There is also a more practical FreeAndNIl (objects), it is not only free up memory, and the reference object (variable) values ​​assigned nil

Fully custom class constructor and destructive

Customizations can also be able to encode their own constructors and destructive functions, to allocate memory, release memory, and use pointers, which are high complexity, and the custom Class is more convenient.

Class package

Package meanings: the individual cell (objects and classes) of all the functions and data (member variables, member functions, attributes) comprising all its interior.

The purpose of the package: prevent the outside world from destroying the internal structure and data of the object. Only by the external interface can access the data inside the object, the objects of the object cannot be directly accessed directly, and the information must be passed through some information, and this information is called, the object inside the object, or the inside of the object Attributes or fields.

Ways: Delphi Declaration Technology for Class (Class). Interface portions are used to define the acts of object appearance and object external performance, and the implementation is used to handle internal operation within the object, and its contents include unapproved fields, methods, and even attributes. ObjectPascal package level

Five levels

Private

(1) The class members (including fields, methods (class methods) here can only be used within the unit declared.

(2) Subclasses can continue the parent class Private member, but these members cannot be used

This part of the declaration method is a class method, that is, the object cannot be called directly.

Protected

(1) The class member (including fields, methods (class methods) here can only be used within the unit declared unit.

(2) Subclasses can inherit the PROTECTED member of the parent class, but it is also limited to the unit defined unit.

The partial declaration method is also a class method, and the object of the class cannot be called directly.

Public

(1) The visibility of the class member (including fields, methods (object methods) here, can be used in any unit where the class unit and the class object is located.

Published

(1) The visibility is the same as public.

(2) The difference between PUBLIC is that members of this area can appear in the Delphi object inspector, and this difference comes from the RTTI mechanism

RTTI (Run Time Type Information) is a method (including attributes, events), attributes (including properties, and events) for Published to allow applications to dynamically query the Published of the class, attributes (including properties, and events)

The RTTI start condition is conditional-enables the "{$ m }" compile instruction to start the RTTI or its ancestor class is compiled by the above state, or the RTTI can be activated, and then the type of inspector can be displayed in the inspector. .

(3) In addition to the RTTI conditions, the Published properties also have type limits.

A. Sequence, string, clas, interface, and Mothod-Pointer classes, you can do Published properties

B. The set value between 0-31 must meet the Published property of Byte, Word or Double Word classes.

C. In addition to all real classes outside the REAL class, you can do a public property.

D. A number of groups cannot do a public property

E. All member functions can be used as a public event, however more functions overload Override cannot be used as a Published event.

F. Field Fieled can't be used as the properties of the public, unless it belongs to the Class or Interface class

Automated

(1) The visibility is the same as public members.

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

New Post(0)