Speaking of encapsulation, it is actually a matter of basic classes. It provides programming methods for interaction between systems and systems, modules and modules, and classes and classes.
As a junior GIS programmer , Let’s not mention the macro concepts of encapsulation. Programming often faces "fields, properties, methods", which is also object-oriented## One of #Basic Concepts.
1. Field
is usually defined as private, indicating the status information of the class privatestring name;
2. Attribute
is usually defined as public, indicating the external members of the class. Properties are readable and writable, and their read and write control is achieved through get and set accessors. If the property is read-only, just implement the get accessor; if the property is writable, just implement the set accessor. There is also a parameter-containing attribute, which is called an indexin c#. Indexers are generally used to facilitate reference to class instantiated objects.
The code is as follows:public string Name
{
get{
return
name;}
set
{
name=value==
null
?String.Empty:value;//name??String.Empty(左侧为null,则返回右侧操作数值,不为null则返回左侧操作数值)}
}
3. Method Method encapsulates the behavior of the class and provides the external performance of the class. It is used to provide the external
interfacewith the internal details of the encapsulation in public methods. The external interaction methods are usually implemented as public. However, operations within the class are basically implemented in a private manner, ensuring the hiding and protection of internal data. In VS2010, you can also select the code segment → select refactoring → extract method.
The above is the detailed content of Basic learning of C# encapsulation. For more information, please follow other related articles on the PHP Chinese website!