Encapsulation of c basic learning

  • 2020-05-19 05:37:01
  • OfStack

As a beginner GIS programmer, the concept of encapsulating those macros is not mentioned for a moment. Programming is often faced with "fields, properties, methods", which is also the basic concept of object-oriented 1.

1. The field

Usually defined as private, which represents the state information of the class

private string name;

2. The attribute

Usually defined as public, which represents the external members of a class. Properties are readable, writable, and controlled by the get and set accessors. If the property is read-only, only the get accessor is implemented. If the property is writable, the set accessor can be implemented. There is also a parameter property called an indexer in c#. Indexer 1 is generally used to facilitate references to class instantiated objects.


public string Name
{
get{return name;}
set
{
name=value==null?String.Empty:value;//name??String.Empty( On the left side for null , returns the operating value on the right side, not null Returns the left operand value )}
}

In fact, this is directly intelligent in VS2010, select field → select refactor → encapsulate field, so that OK.

Method 3.

Method encapsulates the behavior of the class and provides its external representation. Used to provide external interfaces to encapsulated internal details in public methods, usually implemented as public. However, the operations inside the class are basically implemented in the form of private, which ensures the hiding and protection of the internal data. In VS2010, you can also select code segment → select refactoring → extract method.


Related articles: