Chapter II-delphi Object-oriented Programming (IV.) (1)

Source: Internet
Author: User
Tags abstract define abstract inheritance join

2.1.10.7 the library unit into the project

It is easier to add a library unit to the project. Whether it's your own library unit or the library unit that Delphi has built for the form, if it's done, open the project you want to join the library unit (you can open the project with open project); then choose file| Open File, and then select the source program you want to join (. Pas file), and select OK. The library unit is added to the application.

2.2 Programming with Delphi Object

Delphi is an advanced development environment based on object-oriented programming. Object-oriented Programming (OOP) is a natural extension of structured language. OOP's advanced programming method produces a clear and easily extensible and maintainable program. Once you have created an object for your program, you and other programmers can use the object in other programs without having to reconfigure the cumbersome code at all. The reuse of objects can greatly save development time and effectively improve the efficiency of you and others.

2.2.1 What is an object

An object is a data type. An object, like a record, is a data structure. In the simplest sense, we can interpret the object as a record. But in fact, an object is a term that is undefined, and it is often used to define abstract transactions, which are the items that make up an application, and are much more content than records. In this book, objects can be understood as visual components such as buttons, labels, tables, and so on.

To understand the object, the key is to grasp the characteristics of the object. An object, its most prominent features are three: encapsulation, inheritance, polymorphism.

Encapsulation of the 2.2.1.1 object

The basic understanding of objects is the combination of data and code in the same structure, which is the encapsulation nature of the object. Enclosing the object's data field inside the object makes it necessary for external programs and can only use the correct method to access the data fields to read and write. Encapsulation means that the data and code appear together in the same structure, if necessary, can be built around the data "wall", only the object class method in the "wall" open the gap.

Inheritance of 2.2.1.2 objects

The meaning of inheritance is direct and obvious. It refers to the definition of a new object as a descendant of an existing object, and the new object inherits everything from the old class. Before you add any new content to the new object, each field and method of the parent class already exists in the subclass, and the parent class is the cornerstone of the creation of the child class.

Polymorphism of 2.2.1.3 objects

Polymorphism is the means to separate the idea from the realization in the object system. If inheritance is the layout means of the system, polymorphism is the method of realizing its function. Polymorphism means that some sort of generalized action can be implemented in a particular way, depending on the object that executes the action. Polymorphism allows similar objects to be handled in a similar way in a class system. Depending on the specific task, an application is decomposed into many objects, and polymorphism puts the idea of high-level design processing, such as the creation of new objects, the display of objects on screen, and other abstract descriptions of the program running, to be implemented by objects that know how to handle them perfectly.

2.2.1.4 through the Delphi instance to understand the object

Let us combine the Delphi example to discuss the concept of the object:

When you want to build a new project, Delphi will display a form as the basis for the design. In the program editor, Delphi describes the form as a new object type and generates the program code to create the new Form object in the library unit associated with the form.

Unit Unit1;

Interface

Uses Sysutils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs;

Type

TForm1 = Class (Tform) {Start of type description for form}

Private

{Private declarations}

Public

{Public declarations}

End {Type description end of form}

Var

Form1:tform1; {Description of a form variable}

Implementation

{$R *. DFM}

End.

The new form type is TForm1, which is an object inherited from Tform. It has the characteristics of an object: contains a field or method. Because you did not add any parts to the form, it only has fields and methods that are inherited from the Tform class, and you do not see any fields, methods, or descriptions in the type description of the form object. Form1 is called an instance of the TForm1 type (instance). You can describe instances of multiple object types, such as when managing multiple child windows in a multiple-document interface (MDI). Each instance has its own description, but all instances share the same code.

Suppose you add a button part to the form and create an OnClick event handler for the button. To view the source program for the UNIT1, you will find that the TForm1 type description is as follows:

Type

TForm1 = Class (Tform)

Button1:tbutton;

Procedure Button1Click (Sender:tobject);

Private

{Private declarations}

Public

{Public declarations}

End

The TForm1 object now has a field named Button1: It is the button you added to the form. TButton is an object type, and Button1 is an instance of TButton. It is contained by the TForm1 object as its data field. Each time you add a part to a form, the name of the part is added to the type description as a TFom1 field. In Delphi, the event-handling process that you write is a method of the Form object. Each time you create an event-handling process, a method is described in the form's object type.

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.