Class Structure Member-attribute

Source: Internet
Author: User

Class Structure Member-attributes

1. Describe an object in terms of fields and Methods

2. attribute is actually a way to access a field (essentially a method)

1,

Attributes are defined in a class to provide a flexible mechanism for reading, writing, or calculating private field values. Therefore, an attribute generally has a private field corresponding to the attribute.

 
For example, the class testclass {string name; // Private field can be accessed only in the middle of the class, and external members cannot access the public string name // attribute, you can read and write the corresponding private field {get {return name;} // The get accessor provides the set {name = value;} // sets the private value }}

 

Read and Write private fields externally through Properties

Testclass ts1 = new testclass ();
// Assign values to attributes
Ts1.name = "dedeyi ";
// Read the attribute value
Console. writeline (ts1.name );

2. The essence of attributes is method.

For example, the class testclass {string name; // Private field can be accessed only in the middle of the class, and external members cannot access public string get_name () {return name ;} public void set_name (string name) {This. name = Name ;}
 
Public string name // attribute is open to the outside, which can be used to read and write the corresponding private field {
 
Get {return name;} // get accessors provide the set {name = value;} // sets the private value }}

 

Add and retrieve in the original class, set the field name, the compiler will report an error, but we have not defined other get_name (), set_name (), methods ah!


However, through decompilation, we can see that there are two methods in testclass that replace the attributes. These two methods are exactly get_name () and set_name ().

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.