C#類中的屬性使用總結(jié)(詳解類的屬性)
private int dd;
public int dd
{
get{ return xx*3;}
set{ xx = value/3;}
}
沒有set的屬性是一種只讀屬性,沒有g(shù)et的訪問器是一種只寫屬性。
(1) get訪問器用來返回字段或者計(jì)算 并返回字段,它必須以return或者throw終結(jié)。
private string name;
public string Name
{
get
{
return name != null ? name : "NA";
}
}
(2) set訪問器類似返回類型為void的函數(shù),使用value的隱式參數(shù)
private string name; // the name field
public string Name // the Name property
{
get
{
return name;
}
set
{
name = value;
}
}
(3) 訪問器的限制
屬性訪問標(biāo)記可以為public,private,protected,internal,protected internal,因?yàn)樵L問器的訪問限制必須比屬性的訪問限制更加嚴(yán)格,所以
private int xx;
public int sxx
{
public get { return xx; }//error
set { xx = value; }
}
不能對接口或者顯式的接口使用訪問修飾符,因?yàn)榻涌诶锢锩嫠械哪J(rèn)是public的;
同時(shí)具有g(shù)et,set訪問器時(shí),才允許使用訪問修飾符,并且只能有一個(gè)使用;
如果屬性有override修飾的時(shí)候,訪問器修飾符必須與被重寫的匹配。
訪問器的可訪問級別必須比屬性的可訪問級別更加嚴(yán)格
理解:
首先第四條最容易想到,也是很合理的,畢竟是外圍的決定內(nèi)部的。
其次,既然第四條可以理解,那么如果只有一個(gè)訪問器的時(shí)候,訪問器訪問級別等同屬性,如果這個(gè)時(shí)候再去指 定更加嚴(yán)格的訪問級別,那么為何不當(dāng)初在屬性上指定呢?
這條理解了,那么為什么必須同時(shí)具有g(shù)et,set才能添加訪問修飾符就更加明確了。
推理:
接口中屬性是public的,那么如果接口中只有一個(gè)get或者set的時(shí)候,我們可以在繼承中指明另一個(gè)訪問器的屬 性。但是如果接口中同時(shí)具有g(shù)et,set,那么按照派生和繼承的匹配性,這時(shí)候就不能這樣再指定訪問器的訪問限制了。
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í)訪問
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來修飾屬性,派生類使用override來重寫屬性
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 修飾屬性,派生類來實(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 修飾屬性,派生類不能修改屬性
(9)接口屬性
接口屬性不具有函數(shù)體
public interface Inters
{
string Name
{
get;
set;
}
}
(10) 自動(dòng)屬性
當(dāng)屬性訪問器中不需要其他訪問邏輯時(shí)候,就可以使用自動(dòng)屬性,使代碼更加簡潔
public double TotalPurchases { get; set; }
public string Name { get; set; }
public int CustomerID { get; set; }
相關(guān)文章
c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入
這篇文章主要介紹了c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入的步驟,幫助大家更好的理解和使用c#中的Dictionary類,感興趣的朋友可以了解下2021-02-02
C#中按引用傳遞與按值傳遞的區(qū)別,以及ref與out關(guān)鍵字的用法詳解
以下是對C#中按引用傳遞與按值傳遞的區(qū)別,以及ref與out關(guān)鍵字的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-07-07
C#使用NPOI設(shè)置Excel下拉選項(xiàng)
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI設(shè)置Excel下拉選項(xiàng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
C# 獲取當(dāng)前總毫秒數(shù)的實(shí)例講解
這篇文章主要介紹了C# 獲取當(dāng)前總毫秒數(shù)的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程(下)
這篇文章主要介紹了深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程的相關(guān)知識(shí),文中講解的非常詳細(xì),幫助大家更好的學(xué)習(xí)c#網(wǎng)絡(luò)編程,感興趣的朋友可以了解下2020-06-06

