Understanding Delphi classes (12)-in-depth attributes [1]-First Review

Source: Internet
Author: User
There are many topics about attributes, such as array attributes, default attributes, and class attributes.
Summarize the attributes mentioned above.
 
Tmyclass = Class (tobject) Private fname: string; Procedure setname (const value: string); published property name: String read fname write setname; end;

  

1. properties are defined by property;

2. There must be at least one read or write keyword. If only one keyword exists, it becomes a read-only attribute or a write-only attribute;

3. You must specify a type after the attribute name (either a built-in type or a pre-defined type). The access type must be the same as the type;

4. The fields and methods used by the attribute must be declared before the attribute;

5. If the read attribute is a method, this method must be a function and the return value is consistent with the attribute type;
If the attribute is a non-array attribute, this function has no parameters, for example, function getname: string;

6. If the attribute write is a method (generally not a field), this method must be a process;
If the attribute is not an array attribute, there must be only one parameter in this process. The parameter type must be the same as that of the attribute, and the parameter must be a value;
For example: Procedure setname (S: string); or procedure setname (const S: string );

7. The method called by properties cannot be a virtual method or a dynamic method; it cannot be a overloaded method;

8. If the attribute is in the published area, the method it calls can only use the default register call Convention;

9. attributes are generally placed in the published area. If the attribute is not public, the meaning of the attribute is lost;

10. If the custom class does not require rtti, it may save a lot of time to put the attribute in the public area (I personally think );

11. variables or methods for reading and writing attributes are generally placed in the private area. If they are made public, attributes are not needed;

12. Read and Write event properties must be methods, not variables;

13. The attribute is just a bridge, and it does not have a pointer (the variables and methods it reads and writes have pointers). The purpose is to facilitate access and propose access restrictions;

14. The IDE of Delphi provides convenient shortcut keys for writing attributes, such:
Enter: property name: string; then execute Ctrl + Shift + c... wow!

--- Attributes are a wonderful idea. The read/write attributes are as convenient as the read/write structure. However, there are many rules for attributes, but they are only the rules of general attributes.

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.