C#属性

C# 属性(Property)

属性(Property) 是类(class)、结构(structure)和接口(interface)的命名(named)成员。类或结构中的成员变量或方法称为 域(Field)。属性(Property)是域(Field)的扩展,且可使用相同的语法来访问。它们使用 访问器(accessors) 让私有域的值可被读写或操作。

属性(Property)不会确定存储位置。相反,它们具有可读写或计算它们值的 访问器(accessors)

例如,有一个名为 Student 的类,带有 age、name 和 code 的私有域。我们不能在类的范围以外直接访问这些域,但是我们可以拥有访问这些私有域的属性。

访问器(Accessors)

属性(Property)的访问器(accessor)包含有助于获取(读取或计算)或设置(写入)属性的可执行语句。访问器(accessor)声明可包含一个 get 访问器、一个 set 访问器,或者同时包含二者。例如:

[csharp]  view plain  copy
  1. // 声明类型为 string 的 Code 属性  
  2. public string Code  
  3. {  
  4.    get  
  5.    {  
  6.       return code;  
  7.    }  
  8.    set  
  9.    {  
  10.       code = value;  
  11.    }  
  12. }  
  13.   
  14. // 声明类型为 string 的 Name 属性  
  15. public string Name  
  16. {  
  17.    get  
  18.    {  
  19.      return name;  
  20.    }  
  21.    set  
  22.    {  
  23.      name = value;  
  24.    }  
  25. }  
  26.   
  27. // 声明类型为 int 的 Age 属性  
  28. public int Age  
  29. {   
  30.    get  
  31.    {  
  32.       return age;  
  33.    }  
  34.    set  
  35.    {  
  36.       age = value;  
  37.    }  
  38. }  

下面的实例演示了属性(Property)的用法:

[csharp]  view plain  copy
  1. using System;  
  2. namespace tutorialspoint  
  3. {  
  4.    class Student  
  5.    {  
  6.   
  7.       private string code = "N.A";  
  8.       private string name = "not known";  
  9.       private int age = 0;  
  10.   
  11.       // 声明类型为 string 的 Code 属性  
  12.       public string Code  
  13.       {  
  14.          get  
  15.          {  
  16.             return code;  
  17.          }  
  18.          set  
  19.          {  
  20.             code = value;  
  21.          }  
  22.       }  
  23.      
  24.       // 声明类型为 string 的 Name 属性  
  25.       public string Name  
  26.       {  
  27.          get  
  28.          {  
  29.             return name;  
  30.          }  
  31.          set  
  32.          {  
  33.             name = value;  
  34.          }  
  35.       }  
  36.   
  37.       // 声明类型为 int 的 Age 属性  
  38.       public int Age  
  39.       {  
  40.          get  
  41.          {  
  42.             return age;  
  43.          }  
  44.          set  
  45.          {  
  46.             age = value;  
  47.          }  
  48.       }  
  49.       public override string ToString()  
  50.       {  
  51.          return "Code = " + Code +", Name = " + Name + ", Age = " + Age;  
  52.       }  
  53.     }  
  54.     class ExampleDemo  
  55.     {  
  56.       public static void Main()  
  57.       {  
  58.          // 创建一个新的 Student 对象  
  59.          Student s = new Student();  
  60.               
  61.          // 设置 student 的 code、name 和 age  
  62.          s.Code = "001";  
  63.          s.Name = "Zara";  
  64.          s.Age = 9;  
  65.          Console.WriteLine("Student Info: {0}", s);  
  66.          // 增加年龄  
  67.          s.Age += 1;  
  68.          Console.WriteLine("Student Info: {0}", s);  
  69.          Console.ReadKey();  
  70.        }  
  71.    }  
  72. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. Student Info: Code = 001, Name = Zara, Age = 9  
  2. Student Info: Code = 001, Name = Zara, Age = 10  

抽象属性(Abstract Properties)

抽象类可拥有抽象属性,这些属性应在派生类中被实现。下面的程序说明了这点:

[csharp]  view plain  copy
  1. using System;  
  2. namespace tutorialspoint  
  3. {  
  4.    public abstract class Person  
  5.    {  
  6.       public abstract string Name  
  7.       {  
  8.          get;  
  9.          set;  
  10.       }  
  11.       public abstract int Age  
  12.       {  
  13.          get;  
  14.          set;  
  15.       }  
  16.    }  
  17.    class Student : Person  
  18.    {  
  19.   
  20.       private string code = "N.A";  
  21.       private string name = "N.A";  
  22.       private int age = 0;  
  23.   
  24.       // 声明类型为 string 的 Code 属性  
  25.       public string Code  
  26.       {  
  27.          get  
  28.          {  
  29.             return code;  
  30.          }  
  31.          set  
  32.          {  
  33.             code = value;  
  34.          }  
  35.       }  
  36.      
  37.       // 声明类型为 string 的 Name 属性  
  38.       public override string Name  
  39.       {  
  40.          get  
  41.          {  
  42.             return name;  
  43.          }  
  44.          set  
  45.          {  
  46.             name = value;  
  47.          }  
  48.       }  
  49.   
  50.       // 声明类型为 int 的 Age 属性  
  51.       public override int Age  
  52.       {  
  53.          get  
  54.          {  
  55.             return age;  
  56.          }  
  57.          set  
  58.          {  
  59.             age = value;  
  60.          }  
  61.       }  
  62.       public override string ToString()  
  63.       {  
  64.          return "Code = " + Code +", Name = " + Name + ", Age = " + Age;  
  65.       }  
  66.    }  
  67.    class ExampleDemo  
  68.    {  
  69.       public static void Main()  
  70.       {  
  71.          // 创建一个新的 Student 对象  
  72.          Student s = new Student();  
  73.               
  74.          // 设置 student 的 code、name 和 age  
  75.          s.Code = "001";  
  76.          s.Name = "Zara";  
  77.          s.Age = 9;  
  78.          Console.WriteLine("Student Info:- {0}", s);  
  79.          // 增加年龄  
  80.          s.Age += 1;  
  81.          Console.WriteLine("Student Info:- {0}", s);  
  82.          Console.ReadKey();  
  83.        }  
  84.    }  
  85. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. Student Info: Code = 001, Name = Zara, Age = 9  
  2. Student Info: Code = 001, Name = Zara, Age = 10  

C# 属性(Property)

属性(Property) 是类(class)、结构(structure)和接口(interface)的命名(named)成员。类或结构中的成员变量或方法称为 域(Field)。属性(Property)是域(Field)的扩展,且可使用相同的语法来访问。它们使用 访问器(accessors) 让私有域的值可被读写或操作。

属性(Property)不会确定存储位置。相反,它们具有可读写或计算它们值的 访问器(accessors)

例如,有一个名为 Student 的类,带有 age、name 和 code 的私有域。我们不能在类的范围以外直接访问这些域,但是我们可以拥有访问这些私有域的属性。

访问器(Accessors)

属性(Property)的访问器(accessor)包含有助于获取(读取或计算)或设置(写入)属性的可执行语句。访问器(accessor)声明可包含一个 get 访问器、一个 set 访问器,或者同时包含二者。例如:

[csharp]  view plain  copy
  1. // 声明类型为 string 的 Code 属性  
  2. public string Code  
  3. {  
  4.    get  
  5.    {  
  6.       return code;  
  7.    }  
  8.    set  
  9.    {  
  10.       code = value;  
  11.    }  
  12. }  
  13.   
  14. // 声明类型为 string 的 Name 属性  
  15. public string Name  
  16. {  
  17.    get  
  18.    {  
  19.      return name;  
  20.    }  
  21.    set  
  22.    {  
  23.      name = value;  
  24.    }  
  25. }  
  26.   
  27. // 声明类型为 int 的 Age 属性  
  28. public int Age  
  29. {   
  30.    get  
  31.    {  
  32.       return age;  
  33.    }  
  34.    set  
  35.    {  
  36.       age = value;  
  37.    }  
  38. }  

下面的实例演示了属性(Property)的用法:

[csharp]  view plain  copy
  1. using System;  
  2. namespace tutorialspoint  
  3. {  
  4.    class Student  
  5.    {  
  6.   
  7.       private string code = "N.A";  
  8.       private string name = "not known";  
  9.       private int age = 0;  
  10.   
  11.       // 声明类型为 string 的 Code 属性  
  12.       public string Code  
  13.       {  
  14.          get  
  15.          {  
  16.             return code;  
  17.          }  
  18.          set  
  19.          {  
  20.             code = value;  
  21.          }  
  22.       }  
  23.      
  24.       // 声明类型为 string 的 Name 属性  
  25.       public string Name  
  26.       {  
  27.          get  
  28.          {  
  29.             return name;  
  30.          }  
  31.          set  
  32.          {  
  33.             name = value;  
  34.          }  
  35.       }  
  36.   
  37.       // 声明类型为 int 的 Age 属性  
  38.       public int Age  
  39.       {  
  40.          get  
  41.          {  
  42.             return age;  
  43.          }  
  44.          set  
  45.          {  
  46.             age = value;  
  47.          }  
  48.       }  
  49.       public override string ToString()  
  50.       {  
  51.          return "Code = " + Code +", Name = " + Name + ", Age = " + Age;  
  52.       }  
  53.     }  
  54.     class ExampleDemo  
  55.     {  
  56.       public static void Main()  
  57.       {  
  58.          // 创建一个新的 Student 对象  
  59.          Student s = new Student();  
  60.               
  61.          // 设置 student 的 code、name 和 age  
  62.          s.Code = "001";  
  63.          s.Name = "Zara";  
  64.          s.Age = 9;  
  65.          Console.WriteLine("Student Info: {0}", s);  
  66.          // 增加年龄  
  67.          s.Age += 1;  
  68.          Console.WriteLine("Student Info: {0}", s);  
  69.          Console.ReadKey();  
  70.        }  
  71.    }  
  72. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. Student Info: Code = 001, Name = Zara, Age = 9  
  2. Student Info: Code = 001, Name = Zara, Age = 10  

抽象属性(Abstract Properties)

抽象类可拥有抽象属性,这些属性应在派生类中被实现。下面的程序说明了这点:

[csharp]  view plain  copy
  1. using System;  
  2. namespace tutorialspoint  
  3. {  
  4.    public abstract class Person  
  5.    {  
  6.       public abstract string Name  
  7.       {  
  8.          get;  
  9.          set;  
  10.       }  
  11.       public abstract int Age  
  12.       {  
  13.          get;  
  14.          set;  
  15.       }  
  16.    }  
  17.    class Student : Person  
  18.    {  
  19.   
  20.       private string code = "N.A";  
  21.       private string name = "N.A";  
  22.       private int age = 0;  
  23.   
  24.       // 声明类型为 string 的 Code 属性  
  25.       public string Code  
  26.       {  
  27.          get  
  28.          {  
  29.             return code;  
  30.          }  
  31.          set  
  32.          {  
  33.             code = value;  
  34.          }  
  35.       }  
  36.      
  37.       // 声明类型为 string 的 Name 属性  
  38.       public override string Name  
  39.       {  
  40.          get  
  41.          {  
  42.             return name;  
  43.          }  
  44.          set  
  45.          {  
  46.             name = value;  
  47.          }  
  48.       }  
  49.   
  50.       // 声明类型为 int 的 Age 属性  
  51.       public override int Age  
  52.       {  
  53.          get  
  54.          {  
  55.             return age;  
  56.          }  
  57.          set  
  58.          {  
  59.             age = value;  
  60.          }  
  61.       }  
  62.       public override string ToString()  
  63.       {  
  64.          return "Code = " + Code +", Name = " + Name + ", Age = " + Age;  
  65.       }  
  66.    }  
  67.    class ExampleDemo  
  68.    {  
  69.       public static void Main()  
  70.       {  
  71.          // 创建一个新的 Student 对象  
  72.          Student s = new Student();  
  73.               
  74.          // 设置 student 的 code、name 和 age  
  75.          s.Code = "001";  
  76.          s.Name = "Zara";  
  77.          s.Age = 9;  
  78.          Console.WriteLine("Student Info:- {0}", s);  
  79.          // 增加年龄  
  80.          s.Age += 1;  
  81.          Console.WriteLine("Student Info:- {0}", s);  
  82.          Console.ReadKey();  
  83.        }  
  84.    }  
  85. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. Student Info: Code = 001, Name = Zara, Age = 9  
  2. Student Info: Code = 001, Name = Zara, Age = 10  

猜你喜欢

转载自blog.csdn.net/weixin_39029925/article/details/78212199