Encapsulation: C # illustration-benefits of using attributes

Source: Internet
Author: User
Introduction  For a programmer with rich experience in structured programming, object-oriented programming may bring a very unnatural feeling to them. Encapsulation is the first step in implementing object-oriented programming. encapsulation is to aggregate data or functions in units (called classes ). Encapsulated objects are generally called abstract data types. In this article, we will learn the attributes in detail. Encapsulation meaning  The significance of encapsulation is to protect or prevent code (data) from being accidentally damaged. In object-oriented programming, data is regarded as a central element and is closely integrated with functions that use it, thus protecting it from being accidentally modified by other functions. Encapsulation provides an effective way to protect data from Accidental damages. Compared to defining data (implemented using domains) as public in a program, we can define them as private in many ways. Private data can be indirectly controlled in two ways. Let's take a look at some C # examples to learn these two methods to encapsulate data. First, we use traditional storage and retrieval methods. The second method is property ). No matter which method we use, our goal is to use data without any damages or changes. Encapsulated with traditional read and write Methods  Let's take a look at an example of a class department. to manipulate the data (string partition name) in this class, we define a read method and a write method. Using system;
Public class Department
{
Private string CommonName;
.......

// Read Method
Public String getaskname ()
{
Return response name;
}

// Write Method
Public void setdepartname (string)
{
CommonName =;
}

} Using the above method, we can protect private data from being damaged by external programs. Now we use two different methods to write and read data public static int main (string [] ARGs)
{
Department d = new Department ();
D. setcategory name ("electronics ");
Console. writeline ("the Department is:" + D. getpipeline name ());
Return 0;
} In the above example, we cannot directly access the private data (string parameter name) in instance D of the Department class. We can only access the data using these two methods.Encapsulate with attributes Attribute is a language component introduced by C #. Only a few languages Support attributes. Read and Write attributes to protect the fields in the class. The first method is also a good method, but it is more convenient to encapsulate attributes. Let's take an example: using system;
Public class Department
{
Private string CommonName;
Public String CommonName
{
Get
{
Return response name;
}
Set
{
Counter name = value;
}
}
}
Public class departmentmain
{
Public static int main (string [] ARGs)
{
Department d = new Department ();
D. Organization name = "communication ";
Console. writeline ("the Department is: {0}", D. Subject name );
Return 0;
}
} Through the example above, we can see how to implement encapsulation through attributes. The property has two types of operations: Get and set. Get is used to return the value of the attribute field. Set assigns a value to the attribute field through the value variable. The attribute can be set to read-only ). This requires only one set operation for the attribute.Read-only attribute Using system;
Public class readdepartment
{
Private string CommonName;
Public readdepartment (string avalue)
{
CommonName = avalue;
}
Public String CommonName
{
Get
{
Return response name;
}
}
}
Public class readpolicmain
{
Public static int main (string [] ARGs)
{
Readdepartment d = new readdepartment ("computerscience ");
Console. writeline ("the Department is: {0}", D. Subject name );
Return 0;
}
} In the above example, we see how to implement a read-only attribute. The readdepartment class has a get attribute. The write operation is omitted. This special class has a constructor that accepts a string variable. A new object d is created for the main method in the readpolicmain class. For an instance like D, the readdepartment class uses a constructor with a string parameter. Because the above attribute is read-only, we do not assign a value to the domain name and we only read the value in this field. Of course, the attribute can also be write-only, which only requires the attribute to have only one get operation.Write only attributes Using system;
Public class writedepartment
{
Private string CommonName;

Public String CommonName
{
Set
{
Counter name = value;
Console. writeline ("the Department is: {0}", Subject name );
}
}
}
Public class writepolicmain
{
Public static int main (string [] ARGs)
{
Writedepartment d = new writedepartment ();
D. Compile name = "computerscience ";
Return 0;
}
} In the above example, we see how to implement a write-only attribute. The writedepartment class has a parameter name attribute that only implements the set operation. The read operation is omitted.SummaryEncapsulation is the first step towards object-oriented programming. This article shows you some encapsulated knowledge. The traditional read and write methods can be used to implement encapsulation, And the other method to implement encapsulation is to use attributes.Benefits of using attributesThe object user can use a statement to operate internal data.

 

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.