Object Pascal class (5)

Source: Internet
Author: User

Field

In short,Class fieldIt is the variables declared in the class Declaration. They can be seen as variables with class scopes. Besides the class field access level, they can be declaredPrivate(Private ),Public(Public) orProtection(Protected) and beyond control, the field nature of the class is the same as that of the record type field. Private and protected fields are private for the class and invisible outside the class. However, for public fields, only one object is required and can be accessed outside the class.

PreviousTmyrectFor example, it does notPublic(Public) field, so the following code will show a compiler error"Undeclared identifier: 'left'"

Rect2: = tmyrect. createval (0, 0,100,100); {100*100 size rectangle} rect2.left: = 0;

Note

IfTmyrectThe class declaration is in an independent unit. The access to private fields discussed earlier is forbidden and a compiler error will occur.TmyrectThe class description and usage are in the same unit, and the access to its private field will be allowed, including the class in the same unit hasYouyuanPrivilege, that is, different classes can access each other's private fields.

Object PascalUseAttributeThe attribute can be read/write, read-only, or write-only. When the attribute is read, a read method is called, when the attribute is written, it calls a write method. These read and write methods are called in any access attribute. The write method is especially important because it can be used to verify the input or execute other tasks after the attribute is assigned a value. In this way, private fields are always accessed through attributes, but they are not directly accessed. We will discuss"Attribute.

When you create a class instance, each instance has its own data. For example:

  Rect1 := TMyRect.CreateVal(100, 100, 500, 500);  Rect2 := TMyRect.CreateVal(0, 0, 100, 100);

The above Code createsTmyrectAlthough the two instances are completely consistent in structure, they are completely independent in the memory, and each instance has its own data.

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.