C# 属性(Property)
属性(Property) 是类(class)、结构(structure)和接口(interface)的命名(named)成员。类或结构中的成员变量或方法称为 域(Field)。属性(Property)是域(Field)的扩展,且可使用相同的语法来访问。它们使用 访问器(accessors) 让私有域的值可被读写或操作。
属性(Property)不会确定存储位置。相反,它们具有可读写或计算它们值的 访问器(accessors)。
例如,有一个名为 Student 的类,带有 age、name 和 code 的私有域。我们不能在类的范围以外直接访问这些域,但是我们可以拥有访问这些私有域的属性。
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
| public string Code { get { return code; } set { code = value; } }
public string Name { get { return name; } set { name = value; } }
public int Age { get { return age; } set { age = value; } }
|
抽象属性(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
| using System; namespace runoob { public abstract class Person { public abstract string Name { get; set; } public abstract int Age { get; set; } } class Student : Person {
private string code = "N.A"; private string name = "N.A"; private int age = 0;
public string Code { get { return code; } set { code = value; } } public override string Name { get { return name; } set { name = value; } }
public override int Age { get { return age; } set { age = value; } } public override string ToString() { return "Code = " + Code +", Name = " + Name + ", Age = " + Age; } } class ExampleDemo { public static void Main() { Student s = new Student(); s.Code = "001"; s.Name = "Zara"; s.Age = 9; Console.WriteLine("Student Info:- {0}", s); s.Age += 1; Console.WriteLine("Student Info:- {0}", s); Console.ReadKey(); } } }
|
抽象属性例子代码的简化版(使用C# 6.0 语言新特性)
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
| using System; namespace Demo.cs { class Program { public abstract class Person { public abstract string Name { get; set; } public abstract int Age { get; set; } } public class Student : Person { public string Code { get; set; } = "N.A"; public override string Name { get; set; } = "N.A"; public override int Age { get; set; } = 0; public override string ToString() { return $"Code:{Code},Name:{Name},Age:{Age}"; } }
static void Main(string[] args) { var s = new Student() { Code = "001", Name = "Zara", Age = 10 }; System.Console.WriteLine($"Student Info:={s}");
s.Age++; System.Console.WriteLine($"Student Info:={s}"); } } }
|