C # object-oriented programming for the gentle and newest Learning Series-attribute (12)

Source: Internet
Author: User

Field

A field is the data that the storage class needs to meet its design requirements. A field is a class-related variable.

For example, in the Cat class, private string _ name; _ name is actually a field, which is usually a private variable of the class.

Attribute

An attribute is a method or a pair of methods, but in the code that calls it, it is a field, that is, the attribute is suitable for the use of method calls in the form of fields.

Generally, fields are private, that is, private variables, and attributes are public, that is, public variables. Here _ shoutnum is a private field, while ShoutNum is a public external attribute, because it is external, generally the first letter is capitalized, while the field is generally the first letter and the first letter is added with "_"

The get accessors return the same data type as the declared property, indicating that the internal field value or reference is obtained during the call, the set accessors do not explicitly set parameters, but they have an implicit parameter, which is expressed by the keyword value. Its function is to assign values to internal fields or references when calling properties.

Private int _ shoutnum = 3;

Public int ShoutNum

{

// Get indicates that the value of _ shoutnum can be obtained during external calls.

Get {return _ shoutnum };

// Set indicates that the external user can assign a value to the internal _ shoutnum.

Set {_ shoutnum = value };

}

Thoughts

We changed the modifier of the field to public int _ shoutnum = 3. Can we just read and write the variable? You can skip the attribute instead of the attribute?

Yes. If it is only readable and writable, the role of the attribute is no different from that of the public field. However, we hope that more data is publicly available, this is like our house. We don't want the house to be completely transparent. In this way, what you do at home is clearly visible, without privacy, our house usually has windows and doors, but more is an opaque wall. The doors and windows are public, while the things in the house are private, and the doors and windows are controllable, we do not want everyone to enter from the door or fly back and forth from the window. This is the role of the attribute. If you declare the field as public, this means that the undefended doors and windows can be read or written by the caller at any time. This is a very bad thing. It will be much better if the data is written as an attribute.

It is like installing a screen window for the window, only letting the air and sunlight in, the text flies will not be able to enter, more layer control, more layer protection

Private int _ shoutnum = 3;

Public int ShoutNum

{

Get {return _ shoutnum };

// Control the number of calls. A maximum of five calls are allowed. Layer Control is added and layer protection is enabled.

Set

{

If (value <= 5)

_ Shoutnum = value;

Else

_ Shoutnum = 5;

};

}

  

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.