Home  >  Article  >  Backend Development  >  Basic learning of C# encapsulation

Basic learning of C# encapsulation

怪我咯
怪我咯Original
2017-04-01 11:48:581535browse

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

private

string 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 index

in 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则返回左侧操作数值)}
}
In fact, this is directly intelligent in VS2010. Select the field → select reconstruction → encapsulate the field, and it will be OK.

3. Method Method encapsulates the behavior of the class and provides the external performance of the class. It is used to provide the external

interface

with 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn