屬性(Properties)被命名為類(lèi),結(jié)構(gòu)和接口的成員。類(lèi)或結(jié)構(gòu)中的成員變量或方法稱(chēng)為字段。 屬性是字段的擴(kuò)展,并使用相同的語(yǔ)法訪(fǎng)問(wèn)。它們使用訪(fǎng)問(wèn)器,通過(guò)這些訪(fǎng)問(wèn)器可以讀取,寫(xiě)入或操作私有字段的值。
屬性不指定存儲(chǔ)位置。它們有讀取,寫(xiě)入或計(jì)算其值的訪(fǎng)問(wèn)器。
例如,假設(shè)有一個(gè)名稱(chēng)為Student的類(lèi),其中包含年齡(age),名稱(chēng)(name)和代碼(code)的私有字段。我們無(wú)法從類(lèi)范圍外直接訪(fǎng)問(wèn)這些字段,但是可以擁有訪(fǎng)問(wèn)這些私有字段的屬性。
屬性的訪(fǎng)問(wèn)器包含有助于獲取(讀取或計(jì)算)或設(shè)置(寫(xiě)入)屬性的可執(zhí)行語(yǔ)句。訪(fǎng)問(wèn)器聲明可以包含get訪(fǎng)問(wèn)器和set訪(fǎng)問(wèn)器。例如:
// Declare a Code property of type string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare a Name property of type string:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare a Age property of type int:
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
以下示例演示了如何使用屬性:
using System;
namespace yiibai
{
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
// Declare a Code property of type string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare a Name property of type string:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare a Age property of type int:
public 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()
{
// Create a new Student object:
Student s = new Student();
// Setting code, name and the age of the student
s.Code = "10010";
s.Name = "Maxsu";
s.Age = 24;
Console.WriteLine("Student Info: {0}", s);
//let us increase age
s.Age += 1;
Console.WriteLine("Student Info: {0}", s);
Console.ReadKey();
}
}
}
當(dāng)上述代碼被編譯并執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
Student Info: Code = 10010, Name = Maxsu, Age = 24
Student Info: Code = 10010, Name = Maxsu, Age = 25
抽象類(lèi)可能有一個(gè)抽象屬性,它應(yīng)該在派生類(lèi)中實(shí)現(xiàn)。以下程序說(shuō)明了這一點(diǎn):
using System;
namespace yiibai
{
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;
// Declare a Code property of type string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare a Name property of type string:
public override string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare a Age property of type int:
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()
{
// Create a new Student object:
Student s = new Student();
// Setting code, name and the age of the student
s.Code = "1011";
s.Name = "Maxsu";
s.Age = 21;
Console.WriteLine("Student Info:- {0}", s);
//let us increase age
s.Age += 1;
Console.WriteLine("Student Info:- {0}", s);
Console.ReadKey();
}
}
}
當(dāng)上述代碼被編譯并執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
Student Info:- Code = 1011, Name = Maxsu, Age = 21
Student Info:- Code = 1011, Name = Maxsu, Age = 22