Home > Article > Backend Development > C# Learning Diary 27----Properties
Attributes can be named members of classes, structures, and interfaces. They provide a flexible mechanism to read, write, or calculate the value of private fields. Attributes can be viewed as It is a member of the public field, but it actually defines a special method of "accessor" so that the value of the private field can be read, written or manipulated.
Let’s put it more vividly. For example, we define a person class with a private member private string name; in the outside world, we instantiate a person object per, ask for a name for per, and output this name. Just assign a value to per.name and then output it? ? After the previous study, we know that the private-modified variables in the class cannot be accessed by external objects (it cannot be done directly with per.name="HC666"^_^) Therefore, we can only define a public-modified setname in the class. The getname method uses "HC666" as a parameter to write and output, which is a bit cumbersome. At this time, "attributes" should appear. We can make up for this shortcoming by defining a read-write attribute Name for name.
"Accessor"
The accessor of a property contains executable statements to get or set the property. The declaration of the accessor can contain a get accessor (indicating read-only), or a set accessor (meaning write-only), or both (meaning both read and write). (In the above example, we use the get accessor to output name and the set accessor to write)
Define attributes:
In the above example, we can define for name A read-write attribute Name, use the public modifier that can be accessed by external objects
private string name; //声明name变量 public string Name //声明变量的属性,读写 { get { return name; } //定义读访问器,其实就是一个方法 set { name = value; } //定义写访问器,set 方法有一个隐含的参数value }
And define a read-only property, for example, our age is fixed read-only
private uint age=10; //声明变量 age private string Age //声明变量的属性,只读 { get { return age; } //读访问器 }
Examples of attributes:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test1 { class person { //定义变量name,并且为name定义一个读写属性 private string name; //定义属性 public string Name {//访问器 get { return name; } set { name = value; } //set自带一个value参数 } //定义变量age,并且为age定义一个只读属性 private uint age=10; public uint Age { get { return age; } } } class Program { static void Main(string[] args) { person per = new person(); per.Name = "HC666"; //执行写入属性 Console.WriteLine("我的名字叫:{0}\t今年 {1} 岁了",per.Name,per.Age); //读属性 } } }
Results:
Abstract attributes:
As mentioned before, attributes can make classes, Members of structures and interfaces can naturally also be abstract attributes of abstract classes. Abstract attributes, like abstract methods, are implemented in derived classes.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test1 {//定义一个person抽象类 abstract class person {//定义抽象属性 public abstract string Name {//读写 get; set; } public abstract uint Age {//只读 get; } } //定义派生类 class student : person { private string name; private uint age=10; //实现抽象属性 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"; //执行写入属性 Console.WriteLine("我的名字叫:{0}\t今年 {1} 岁了",stu.Name,stu.Age); //读属性 } } }
The result is the same as the previous example.
The above is the content of C# Learning Diary 27----Attributes. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!