Note: I am more accustomed to calling property an asset rather than an attribute. If you know the meaning of propery, you may understand my point of view.
Just writtenProgramWhen I always think that a field can meet the requirements, why should I use attributes that are difficult to define?
To get and set the de-definition,CodeThe Difference and advantages are unknown.
Example:
Public class myclass
{
Public static string
Private sting _ B = "";
Public String getval
{
Get {retrun _ B ;}
Set {_ B = value ;}
}
}
Myclass. A and myclass. getval may implement the same function. Why must we define attributes?
the advantages of attributes compared with common variables are summarized below:
1. Data Security
because a is a public variable, it can be arbitrarily changed outside the class. To prevent any changes caused by users, attribute get and set can be used to restrict _ B's read-only, write-only or control _ B's maximum or minimum
reference a man's short answer: "In fact, custom variables have implemented attributes by default, but they are only simple to read and write. We need to add more restrictions to the defined attributes based on reading and writing. after you learn more, you will find that many things are added for data security. to put it bluntly, it is a limitation . For example, you must specify the method in an interface (note that it is mandatory and mandatory) to implement "
2. One principle of OOP and programming is Data encapsulation, which means that the field type should not be provided to the outside world in a public manner.
3, In fact, it is not difficult to simply write a program. It is rare to design a program to make the code concise and easy to read, with the highest efficiency. How to ensure the safe operation of the program.