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

Source: Internet
Author: User
Tags range

When you use Object Inspector to change the name of an object (part), the change to that name is reflected in the program. For example, the Name property of Form1 is named Colorbox in Object Inspector, and you will notice that in the Type Description section, the TForm1 of the preceding text is changed to:

Tcolorbox=class (Tform);

And in the Variable Description section, will explain colorbox for the tcolorbox type of variables, automatically generated by Delphi event processing process name automatically changed to Tcolorbox.button1click, but the implementation of your own code is not automatically modified. Therefore, if you write a program before changing the Name property, you must change the name of the object in the event-handling process. Therefore, the original form1.color to be changed to Colorbox.color.

2.2.2 Inherit data and methods from an object

The previous TForm1 type is simple because it contains only domain Button1 and method Button1Click. However, on this form, you can change the size of the form, add or remove the maximum minimized button for the form, or set the form as an MDI interface. For an object that contains only one domain and method, you do not see an explicit support program. When you click on a form or use object selector on the top of object inspector to select the Form1 object, press F1 to see its online help, and you will find all the properties and methods that it inherits to in properties and method. These are described in the Tform type, TForm1 is a subclass of Tform that inherits all of its fields, methods, properties, and events. For example, the color properties of a form are described in Tform. When you add a new form to your project, you are adding a basic model. By constantly adding a part to a form, you define a new form yourself. To customize any object, you will inherit the domain and method from an existing object, creating a subclass of that object. For example, an object TForm1 is described as a subclass of an object Tform and has a basic property or method of a form part. Form1 becomes your own type only when you add a part to a form or write an event-handling procedure.

A more specific object is inherited from a broader or more general object, which is the ancestor of this particular object, which is called the descendants of the ancestors. An object can have only one direct ancestor, but it may have many descendants. Tform are ancestors of the TForm1 type, and all form objects are descendants of Tform.

When you use F1 to view online Help for forms, you will find that Tform is called a component (part). This is because all the parts are objects.

All the parts in this structure are objects. The part type Tcomponent inherits data and program code from the TObject type and has additional properties, methods, events that can be used for special purposes, so the widget can deal directly with the user, record its status and store it in a file, and so on. The control type Tcontrol inherits from the Tcomponent and adds new functionality, such as it can display an object. In the above illustration, although Tcheckbox is not inherited directly by TObject, it still has attributes owned by any object, because in the VCL structure, Tcheckbox still inherits the special object of all functions from TObject, but it also has some unique features that are defined by itself. , such as the record status can be selected.

The scope of the 2.2.3 object

2.2.3.1 about the scope of the object

The scope of an object determines its data field, property value, the range of activities of the method, and the scope of the access. The data fields, property values, and methods described in the Description section of an object are only within the scope of the object, and only the object and its descendants can have them. Although the actual program code for these methods may be in a library cell outside of this object, these methods are still within the scope of the object because they are described in the Description section of the object.

When you write program code to access the object's property values, methods, or fields during event handling for an object, you do not need to precede the identifiers with the name of the object variable. For example, if you add a button and an edit box to a new form, and you write the OnClick event handler for this button:

Procedure Tform1.button1click (Sender:tobject);

Begin

Color: =clfuchsia;

Edit1.color: =cllime;

End

The first line of the statement is FORM1 coloring for the entire form. You can also write as follows:

Form1.color: =clfuchsia;

But you don't have to add Form1, because the Button1Click method is in the scope of the TForm1 object. When you are in the scope of an object, you can omit the property values, methods, and object identifiers that precede the fields in all of this object. But when you write a second statement that changes the background of the edit box, because you want to access the color property of the TEdit1 object at this point instead of the TForm1 type, you need to indicate the range of the color property value by adding the name of the edit box before the attribute. If not specified, Delphi will change the color of the form to green as the first statement. Because the Edit1 part is in a form, it is a data field of the form, so you also do not have to specify its dependencies.

If the Edit1 is in another form, you need to add the name of the Hull object before the edit box. For example, if Edit1 is in Form2, it is a data field of the Form2 description and is in the Form2 range, then you need to change the second sentence to read:

Form2.Edit1.Color: = Cllime;

And you need to add Unit2 to the UNIT1 uses clause.

The scope of an object extends to all descendants of this object. All property values, methods, and events for Tform are in the TForm1 range because TForm1 is the descendant of Tform. Your application cannot describe the type, variable, and so on that have the same name as the ancestor's data field. If Delphi shows the information that an identifier is repeatedly defined, it is possible that a data field has the same name as a data field of its ancestor object (such as Tform). You can try to change the name of this identifier.

2.2.3.2 overload a method

You can overload (Override) a method. You can overload a method by describing a method that has the same name as the ancestor object in the descendant object. You can overload this method if you want to make this method work the same as the ancestor object in a descendant object but use a different approach. Delphi does not recommend you to overload the method often unless you want to create a new part. Overload a method, the Delphi compiler does not give an error or warning message.

Description of 2.2.4 object public domain and private domain

When you use the Delphi environment to build your application, you can add data fields and methods to a Tform descendant object, or you can add fields and methods to an object by modifying the object type description directly, rather than adding a part to the form or event handler.

You can add new data fields and methods to the public or private part of an object. Public and private are reserved words of Object Pascal. When you add a new form to the project, Delphi starts to create the new Form object. Each new object contains public and private instructions so that you can add data fields and methods to your code. A data field or method in the public section that describes the methods of an object in another library unit that can also be accessed. The description in the private section has access restrictions. If you describe the domain and method in private, it is opaque outside the library unit of the object and cannot be accessed. Private can be used to describe methods that can be accessed only by the data fields and the library unit objects that are accessed by this library unit method. Program code for a procedure or function can be placed in the implementation portion of the library unit.

2.2.5 access to the domain and method of an object

When you want to change a property of a field of a Form object, or a method that calls it, you must add the name of the object before the property name or the method to invoke it. For example, if you have an edit box part on your form and you need to change its Text property in the run, you need to write the following code:

Edit1.text: = ' Welcome to Delphi ';

Similarly, to clear the selected text in the edit box part, you can invoke the appropriate method for the Tedit part:

Edit1.clearselection;

Use the WITH statement to simplify your program if you want to change multiple properties of one object field in a Form object or call multiple methods. The WITH statement can be used in an object as easily as in a record. The following event-handling procedure makes several adjustments to a list box when responding to the OnClick event:

Procedure Tform1.button1click (Sender:tobject);

Begin

Listbox1.clear;

Listbox1.multiselect: =true;

LISTBOX1.ITEM.ADD (' one ');

LISTBOX1.ITEM.ADD (' two ');

LISTBOX1.ITEM.ADD (' Three ');

listbox1.sorted: =ture;

Listbox1.fontstyle: =[fsbold];

ListBox1.Font.Color: =clpurple;

ListBox1.Font.Name: = ' times New Roman ';

Listbox1.scaleby (125,100);

End

If the WITH statement is used, the program is as follows:

Procedure Tform1.button1click (Sender:tobject);

Begin

With (ListBox1) do

Begin

Clear;

MultiSelect: =true;

Item.add (' one ');

Item.add (' two ');

Item.add (' Three ');

Sorted: =ture;

FontStyle: =[fsbold];

Font.Color: =clpurple;

Font.Name: = ' times New Roman ';

Scaleby (125,100);

End

End

Using the WITH statement, you do not have to precede each property or method with a ListBox1 identifier, and within the WITH statement, all attributes or calling methods are within its scope for the listbox.

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.