Chapter II-delphi Object-oriented Programming (iii) (2)

Source: Internet
Author: User
Tags mathematical functions

2.1.9.4 String Type

The string type is in fact a one-dimensional array of characters. When you describe a variable of a string type, you should indicate the size of the string, and here is an example of the string type:

Type

MYSTRING:STRING[15];

Var

myname:mystring;

The variable myname is stated to be a maximum of 15 characters. If you do not specify the size of the string, Delphi considers the string to contain a maximum value of 255 characters. Assigning values to strings can be assigned directly by using a string literal enclosed in single quotes:

MyName: = ' frank.smith ';

or myname: = ' Zhang Ming ';

Because MyName is a mystring variable that can contain 15 characters, the above two variables are valid and a Chinese character can be treated as two characters. When you assign a string variable more value than a defined value, such as myname to ' Franksmith.franklin ', Delphi will only accept the first 15 characters ' Franksmith.fran '. In memory, a string usually takes up a byte more than the size indicated, because the first position is a byte that contains the size of the array. You can use index values to access the character of a string, myname[1] can get the first character of the MyName ' F '.

You can use Delphi's rich operators, procedures, and functions to handle string-type variables and properties. Here are a few common operators and Delphi procedures or functions:

Concat and (+) functions are the same, you can combine multiple strings together to create a larger string; Copy returns a substring in a string; Delete a certain number of characters from a specified position in a string Inserts a string in a string; length returns the lengths of the string; The Pos returns the position of a substring in a string, which is the index value.

2.1.9.5 collection type

A collection type is a group of elements of the same type that must be finite types such as shaping, Boolean, character, enum, and child boundaries. Collection types are useful when checking whether a value belongs to a particular collection. The following routines can describe the use of collection types:

Add an edit box and a button on the form, clear the text in the edit box, add a label labeled "input vowel" to it, add an empty label below the edit box, change the button's default property to True, and the event-handling procedure for creating the button is as follows: Caption

Procedure Tform1.button1click (Sender:tobject);

Type

Tvowels=set of Char;

Var

Vowels:tvowels;

Begin

Vowels: = [' A ', ' e ', ' I ', ' o ', ' u '];

If edit1.text[1] in vowels then

Lable2.caption: = ' is a vowel ';

Else

Lable2.caption: = ' please try again ';

End

To execute this program, enter the letter in the edit box, the result of the expression edit1.text[1] in vowels is Boolean, in is the operator, to determine whether the letter exists in the collection. The input discriminant results are displayed below the edit box. The above is used to the collection type Tvowels.

2.1.9.6 Record Type

A record is a collection of data that your program can access in groups. The following routine illustrates the use of a record type:

Type

Temployee=record

NAME:STRING[20];

yearhired:1990..2000;

salsry:double;

POSITION:STRING[20];

End

Records contain fields that can hold data, and each field has a data type. The record Temployee type above contains four domains. You can illustrate the variables of a record type in the following ways:

Var

Newemployee,promotedemployee:temployee;

You can access a single field for a record in the following ways:

Newemployee.salary: = 1000;

Write the following statement to assign a value to the entire record:

With Promotedemployee do

Begin

Name: = ';

yearhired: = 1993;

Salary: = 2000.00

Position: = ' editor ';

End

Your program can manipulate records as a single entity:

Promptemployee: = NewEmployee;

The above describes the user-commonly used custom types. In Delphi programming, objects are very important user-defined data types. Like records, an object is a structured data type that contains fields (field) of data, as well as procedures and functions as methods. In Delphi, when you add a part to a form, you add a field to the form object; Each part is also an object, and you automatically add a method to the form whenever you create an event-handling process that allows a part to respond to an event. In the 2nd section of this chapter, the Delphi object-oriented programming approach and techniques are described in detail.

2.1.10 Object Pascal's library unit

Units are collections of constants, variables, data types, procedures, and functions, and can be shared by multiple applications. Delphi already has a number of predefined library units that you can use to build your library unit. The Visual Component Library of Delphi consists of several library units that describe objects, parts, and applications that your application uses to design the user interface. For example, when you add a check box to a form, Delphi automatically adds the Stdctrls library unit to your library unit because the Tcheckbox part is described in the Stdctrls Library unit.

When you design your form, Delphi automatically creates a library unit that is related to your form. Your library units do not have to be related to the form, you can either use predefined library units that contain only mathematical functions, or write your own mathematical function library units. All the instructions in a library unit are related to each other, for example, the Cdialogs library unit contains all the instructions for the Normal dialog box used in your application.

Structure of 2.1.10.1 Object Pascal Program Library Unit

The structure of a library unit is the same regardless of whether a library unit is related to a form. The structure is as follows:

Unit < library cell name >

Interface

Uses < selective library Unit list >

{Public Note}

Implementation

Uses < selective library Unit list >

{Private Description}

{Procedure and function's execution section}

initialization {Selective}

{Selective initialization of the program}

End.

2.1.10.2 the interface part of the library unit

Interface is the interface part of a library unit, which determines the visible (accessible) portion of any other library unit or program for this library unit. You can describe variables, constants, data types, procedures, functions, and so on in the interface section. Delphi describes the form data type, form variables, and event handling procedures in this section in the Library unit where you design the form.

Interface the beginning of the interface section of the logo Library unit. The instructions in interface are visible to other library units or applications that you want to use these instructions. A library unit can use the instructions of other units, simply by specifying those library units in the uses clause. For example, you write program code in library A, and you want to invoke a program that is UNITB in the interface section. You can add the name of the library unit B to the uses clause in the interface section of a, and any program in a can invoke the program described in B. Also, if the C library unit appears in the uses clause in the interface section of B, C,a can also invoke the program described in interface by the B, C library unit, even though a does not appear in a. But if B appears in the uses clause of the interface section of a, then library A cannot appear in the uses clause of B's interface. This results in circular access to the library unit. When you attempt to compile, an error message occurs.

Implementation part of 2.1.10.3 Library unit

The implementation part of the implementation contains the procedures, functions, and event handling processes described in interface. This section can have its own additional instructions, but these instructions are private and cannot be invoked by external programs. The function entity that is described in interface must appear in the implementation section, using the title shorthand: Just enter the procedure or function reserved word, followed by the name of the procedure or functions, followed by the implementation part of the program. If you describe any of the implementation in the section, and the title does not appear in the interface section, you must write the entire caption section.

The library unit specified in the uses clause of the implementation section only supplies the program in the Library unit with the program described in its interface. Other library units that use this library unit do not have access to the description of the library unit in the UDES clause in implementation, because the library unit that was performed after implementation is private. So in the example above, if C appears in the implementation part of B, then a cannot use the public part of C unless C appears in the uses clause of a. The circular access that occurs in implementation is permitted by Delphi, and if B appears in the implemetation uses clause of a, then the implementation part of B can also appear a.

2.1.10.4 the initialization section of the library unit

When you initialize the data used by the current library unit, or when you provide data to other applications and library units using the interface section, you can add a initialization section to the library unit and precede the end of the library unit with your initialization statement. When an application uses a library unit, the initialization portion of the library unit is executed before other code. If an application uses more than one library unit, the initialization portion of each library unit is executed before all program code.

2.1.10.5 uses Delphi's visual components and their library units

When you add a visual part to a form, if the part is in a visual part gallery, Delphi automatically adds the library unit name that you want to use in the uses clause in the interface section of your library unit. But some objects do not exist in the Delphi environment, for example, if you want to add a predefined information box to the library unit, you must add the MSGDLG library unit to your uses clause. If you want to use the Tprinter object, you must add the printer library unit to the uses clause. In the online help, you can find the predefined library units to which the object belongs.

To use a function that is described in another library unit, precede the function with the name of the library unit, with the '. ' Number separated. For example, to use the Calculate function described in Unit1 in Unit2, you should use the following method:

Number: = Unit1.calculate (10);

You can add the name of a library unit before any identifiers such as properties, constants, variables, data types, functions, and so on. You can add program code freely in any Delphi library unit, but do not change the program generated by Delphi.

2.1.10.6 Create a new library unit that is not a form-independent

If you want to create a new library unit in your project that is not related to any form, you can choose file| New Unit. When a new library unit is added to the project, the code for the new library unit is as follows:

Unit Unit2;

Interface

Implementation

End.

Delphi will select a name for your library unit based on the number of files in your project, and you can add your program code to the skeleton of the program.

When you compile your project, the newly added library unit is compiled into a. DCU suffix of the file. This newly generated file is the machine code on the executable file that is linked to the project.

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.