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.