private int dd;
public int dd
{
get{ return xx*3;}
set{ xx = value/3;}
}
沒(méi)有set的屬性是一種只讀屬性,沒(méi)有g(shù)et的訪問(wèn)器是一種只寫(xiě)屬性。
(1) get訪問(wèn)器用來(lái)返回字段或者計(jì)算 并返回字段,它必須以return或者throw終結(jié)。
private string name;
public string Name
{
get
{
return name != null ? name : "NA";
}
}
(2) set訪問(wèn)器類(lèi)似返回類(lèi)型為void的函數(shù),使用value的隱式參數(shù)
private string name; // the name field
public string Name // the Name property
{
get
{
return name;
}
set
{
name = value;
}
}
(3) 訪問(wèn)器的限制
屬性訪問(wèn)標(biāo)記可以為public,private,protected,internal,protected internal,因?yàn)樵L問(wèn)器的訪問(wèn)限制必須比屬性的訪問(wèn)限制更加嚴(yán)格,所以
private int xx;
public int sxx
{
public get { return xx; }//error
set { xx = value; }
}
不能對(duì)接口或者顯式的接口使用訪問(wèn)修飾符,因?yàn)榻涌诶锢锩嫠械哪J(rèn)是public的;
同時(shí)具有g(shù)et,set訪問(wèn)器時(shí),才允許使用訪問(wèn)修飾符,并且只能有一個(gè)使用;
如果屬性有override修飾的時(shí)候,訪問(wèn)器修飾符必須與被重寫(xiě)的匹配。
訪問(wèn)器的可訪問(wèn)級(jí)別必須比屬性的可訪問(wèn)級(jí)別更加嚴(yán)格理解:
首先第四條最容易想到,也是很合理的,畢竟是外圍的決定內(nèi)部的。
其次,既然第四條可以理解,那么如果只有一個(gè)訪問(wèn)器的時(shí)候,訪問(wèn)器訪問(wèn)級(jí)別等同屬性,如果這個(gè)時(shí)候再去指 定更加嚴(yán)格的訪問(wèn)級(jí)別,那么為何不當(dāng)初在屬性上指定呢?
這條理解了,那么為什么必須同時(shí)具有g(shù)et,set才能添加訪問(wèn)修飾符就更加明確了。
推理:
接口中屬性是public的,那么如果接口中只有一個(gè)get或者set的時(shí)候,我們可以在繼承中指明另一個(gè)訪問(wèn)器的屬 性。但是如果接口中同時(shí)具有g(shù)et,set,那么按照派生和繼承的匹配性,這時(shí)候就不能這樣再指定訪問(wèn)器的訪問(wèn)限制了。
public interface ISomeInterface
{
int TestProperty
{
// No access modifier allowed here
// because this is an interface.
get;
}
}
public class TestClass : ISomeInterface
{
public int TestProperty
{
// Cannot use access modifier here because
// this is an interface implementation.
get { return 10; }
// Interface property does not have set accessor,
// so access modifier is allowed.
protected set { }
}
}
(4)可以用static 修飾屬性,以便隨時(shí)訪問(wèn)
private static int counter;
public static int Counter
{
get { return counter; }
}
(5)屬性隱藏
public class Employee
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
public class Manager : Employee
{
private string name;
// Notice the use of the new modifier:
public new string Name // use new to hide property in base class
{
get { return name; }
set { name = value + ", Manager"; }
}
}
(6)virtual來(lái)修飾屬性,派生類(lèi)使用override來(lái)重寫(xiě)屬性
public class Parent
{
public virtual int TestProperty
{
protected set { }
get { return 0; }
}
}
public class Kid : Parent
{
public override int TestProperty
{
protected set { }
get { return 0; }
}
}
(7) abstract 修飾屬性,派生類(lèi)來(lái)實(shí)現(xiàn)屬性
abstract class Shape
{
public abstract double Area
{
get;
set;
}
}
class Square : Shape
{
public double side;
public override double Area
{
get
{
return side * side;
}
set
{
side = System.Math.Sqrt(value);
}
}
}
(8)sealed 修飾屬性,派生類(lèi)不能修改屬性
(9)接口屬性
接口屬性不具有函數(shù)體
public interface Inters
{
string Name
{
get;
set;
}
}
(10) 自動(dòng)屬性
當(dāng)屬性訪問(wèn)器中不需要其他訪問(wèn)邏輯時(shí)候,就可以使用自動(dòng)屬性,使代碼更加簡(jiǎn)潔
public double TotalPurchases { get; set; }
public string Name { get; set; }
public int CustomerID { get; set; }