C # Learning diary 27 ---- attributes

Source: Internet
Author: User

C # Learning diary 27 ---- attributes

Attributes can be named members of classes, structures, and interfaces. They provide flexible mechanisms to read, write, or calculate private attributes) the value of a field. The attribute can be viewed as a member of a public field, however, it actually defines a special method for "accessors" to allow the private field value to be read/written or operated.

Let's talk about the image. For example, we define a person class with a private member private string name. in the outside world, we instantiate a person object per, which requires a name for per, and output this name, that is, per. name assigns a value, and then outputs ?? After learning above, we know that the External Object of the private modified variable in the class cannot be accessed (the external object cannot be directly per. name = HC666 to get ^_^) Therefore, you can only define a public-modified setname and getname method in the class to write and output "HC666" as parameters, at this time, the "attribute" should be launched. we can define a read/write attribute name for the Name to make up for this deficiency.

"Accessors"

The accessors of the attribute include executable statements for obtaining or setting the attribute. The accessors declaration can contain a get accesser (indicating read-only ), or a set accessor (indicating write-only), or two simultaneously contained (indicating readable and writable ). (In the above example, we use get accessors to output name and set accessors to write data)

 

Define attributes:

In the above example, we can define a read/write attribute name for Name and use the public modifier that can be accessed by external objects.

Private string name; // declare the name variable

Public string Name // declare the attributes of a variable, read/write

{

Get {return name;} // defines the read accessors, which is actually a method

Set {name = value;} // defines the write accessors. The set method has an implicit parameter value.

}

Define a read-only attribute. For example, our age is fixed and read-only.

Private uint age = 10; // declare the variable age

Private string Age // declare the attributes of the variable, read-only

{

Get {return age;} // read accessors

}

 

Attribute instance:

 

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test1 {class person {// defines the variable name, and defines a read/write attribute private string name for the name; // define the attribute public string Name {// accessor get {return name;} set {name = value;} // set comes with a value parameter} // define the variable age, define a read-only attribute private uint age = 10 for age; public uint Age {get {return age ;}} class Program {static void Main (string [] args) {person per = new person (); per. name = HC666; // execute the write Attribute Console. writeLine (my name is {0} {1} years old, per. name, per. age); // read attributes }}}


Result:

 

Abstract attributes:

As mentioned above, attributes can be abstract attributes of classes, structures, and interfaces. Abstract attributes are implemented in the same way as abstract methods in the derived classes.

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test1 {// define a person abstract class person {// define the abstract attribute public abstract string Name {// read/write get; set ;} public abstract uint Age {// read-only get ;}// defines the class student: person {private string name; private uint age = 10; // implement the abstract attribute public override string Name {get {return name;} set {name = value ;}} public override uint Age {get {return age ;}}} class Program {static void Main (string [] args) {student stu = new student (); stu. name = HC666; // execute the write Attribute Console. writeLine (my name is {0} {1} years old, stu. name, stu. age); // read attributes }}}


The result is the same as that in the previous example.

 

 

Related Article

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.