C# 属性
属性(Property) 是类(class)、结构(structure)和接口(interface)的命名(named)成员。类或结构中的成员变量或方法称为 域(Field)。属性(Property)是域(Field)的扩展,且可使用相同的语法来访问。它们使用 访问器(accessors) 让私有域的值可被读写或操作。
属性(Property)不会确定存储位置。相反,它们具有可读写或计算它们值的 访问器(accessors)。
例如,有一个名为 Student 的类,带有 age、name 和 code 的私有域。我们不能在类的范围以外直接访问这些域,但是我们可以拥有访问这些私有域的属性。
一、访问器(Accessors)
属性(Property)的访问器(accessor)包含有助于获取(读取或计算)或设置(写入)属性的可执行语句。访问器(accessor)声明可包含一个 get 访问器、一个 set 访问器,或者同时包含二者。例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<span class="hljs-comment">// 声明类型为 string 的 Code 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Code { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> code; } <span class="hljs-keyword">set</span> { code = <span class="hljs-keyword">value</span>; } } <span class="hljs-comment">// 声明类型为 string 的 Name 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> name; } <span class="hljs-keyword">set</span> { name = <span class="hljs-keyword">value</span>; } } <span class="hljs-comment">// 声明类型为 int 的 Age 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> Age { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> age; } <span class="hljs-keyword">set</span> { age = <span class="hljs-keyword">value</span>; } } |
实例
下面的实例演示了属性(Property)的用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">tutorialspoint</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span> code = <span class="hljs-string">"N.A"</span>; <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span> name = <span class="hljs-string">"not known"</span>; <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age = <span class="hljs-number">0</span>; <span class="hljs-comment">// 声明类型为 string 的 Code 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Code { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> code; } <span class="hljs-keyword">set</span> { code = <span class="hljs-keyword">value</span>; } } <span class="hljs-comment">// 声明类型为 string 的 Name 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> name; } <span class="hljs-keyword">set</span> { name = <span class="hljs-keyword">value</span>; } } <span class="hljs-comment">// 声明类型为 int 的 Age 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> Age { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> age; } <span class="hljs-keyword">set</span> { age = <span class="hljs-keyword">value</span>; } } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">string</span> <span class="hljs-title">ToString</span>() </span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">"Code = "</span> + Code +<span class="hljs-string">", Name = "</span> + Name + <span class="hljs-string">", Age = "</span> + Age; } } <span class="hljs-keyword">class</span> <span class="hljs-title">ExampleDemo</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>() </span>{ <span class="hljs-comment">// 创建一个新的 Student 对象</span> Student s = <span class="hljs-keyword">new</span> Student(); <span class="hljs-comment">// 设置 student 的 code、name 和 age</span> s.Code = <span class="hljs-string">"001"</span>; s.Name = <span class="hljs-string">"Zara"</span>; s.Age = <span class="hljs-number">9</span>; Console.WriteLine(<span class="hljs-string">"Student Info: {0}"</span>, s); <span class="hljs-comment">// 增加年龄</span> s.Age += <span class="hljs-number">1</span>; Console.WriteLine(<span class="hljs-string">"Student Info: {0}"</span>, s); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 |
Student Info: Code = <span class="hljs-number">001</span>, Name = Zara, Age = <span class="hljs-number">9</span> Student Info: Code = <span class="hljs-number">001</span>, Name = Zara, Age = <span class="hljs-number">10</span> |
二、抽象属性(Abstract Properties)
抽象类可拥有抽象属性,这些属性应在派生类中被实现。下面的程序说明了这点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">tutorialspoint</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">int</span> Age { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; } } <span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> : <span class="hljs-title">Person</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span> code = <span class="hljs-string">"N.A"</span>; <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span> name = <span class="hljs-string">"N.A"</span>; <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age = <span class="hljs-number">0</span>; <span class="hljs-comment">// 声明类型为 string 的 Code 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Code { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> code; } <span class="hljs-keyword">set</span> { code = <span class="hljs-keyword">value</span>; } } <span class="hljs-comment">// 声明类型为 string 的 Name 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> name; } <span class="hljs-keyword">set</span> { name = <span class="hljs-keyword">value</span>; } } <span class="hljs-comment">// 声明类型为 int 的 Age 属性</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">int</span> Age { <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> age; } <span class="hljs-keyword">set</span> { age = <span class="hljs-keyword">value</span>; } } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">string</span> <span class="hljs-title">ToString</span>() </span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">"Code = "</span> + Code +<span class="hljs-string">", Name = "</span> + Name + <span class="hljs-string">", Age = "</span> + Age; } } <span class="hljs-keyword">class</span> <span class="hljs-title">ExampleDemo</span> { <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>() </span>{ <span class="hljs-comment">// 创建一个新的 Student 对象</span> Student s = <span class="hljs-keyword">new</span> Student(); <span class="hljs-comment">// 设置 student 的 code、name 和 age</span> s.Code = <span class="hljs-string">"001"</span>; s.Name = <span class="hljs-string">"Zara"</span>; s.Age = <span class="hljs-number">9</span>; Console.WriteLine(<span class="hljs-string">"Student Info:- {0}"</span>, s); <span class="hljs-comment">// 增加年龄</span> s.Age += <span class="hljs-number">1</span>; Console.WriteLine(<span class="hljs-string">"Student Info:- {0}"</span>, s); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 |
Student Info: Code = <span class="hljs-number">001</span>, Name = Zara, Age = <span class="hljs-number">9</span> Student Info: Code = <span class="hljs-number">001</span>, Name = Zara, Age = <span class="hljs-number">10</span> |