C#類中的屬性使用總結(jié)(詳解類的屬性)
private int dd;
public int dd
{
get{ return xx*3;}
set{ xx = value/3;}
}
沒有set的屬性是一種只讀屬性,沒有g(shù)et的訪問器是一種只寫屬性。
(1) get訪問器用來返回字段或者計算 并返回字段,它必須以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)格,所以
private int xx;
public int sxx
{
public get { return xx; }//error
set { xx = value; }
}
不能對接口或者顯式的接口使用訪問修飾符,因為接口里里面所有的默認(rèn)是public的;
同時具有g(shù)et,set訪問器時,才允許使用訪問修飾符,并且只能有一個使用;
如果屬性有override修飾的時候,訪問器修飾符必須與被重寫的匹配。
訪問器的可訪問級別必須比屬性的可訪問級別更加嚴(yán)格
理解:
首先第四條最容易想到,也是很合理的,畢竟是外圍的決定內(nèi)部的。
其次,既然第四條可以理解,那么如果只有一個訪問器的時候,訪問器訪問級別等同屬性,如果這個時候再去指 定更加嚴(yán)格的訪問級別,那么為何不當(dāng)初在屬性上指定呢?
這條理解了,那么為什么必須同時具有g(shù)et,set才能添加訪問修飾符就更加明確了。
推理:
接口中屬性是public的,那么如果接口中只有一個get或者set的時候,我們可以在繼承中指明另一個訪問器的屬 性。但是如果接口中同時具有g(shù)et,set,那么按照派生和繼承的匹配性,這時候就不能這樣再指定訪問器的訪問限制了。
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 修飾屬性,以便隨時訪問
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 修飾屬性,派生類來實現(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)屬性訪問器中不需要其他訪問邏輯時候,就可以使用自動屬性,使代碼更加簡潔
public double TotalPurchases { get; set; }
public string Name { get; set; }
public int CustomerID { get; set; }
相關(guān)文章
C#批量插入數(shù)據(jù)到Sqlserver中的三種方式
這篇文章主要為大家詳細(xì)介紹了C#批量插入數(shù)據(jù)到Sqlserver中的三種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12詳解C#使用AD(Active Directory)驗證內(nèi)網(wǎng)用戶名密碼
這篇文章主要介紹了詳解C#使用AD(Active Directory)驗證內(nèi)網(wǎng)用戶名密碼的相關(guān)資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10C#實現(xiàn)托盤程序并禁止多個應(yīng)用實例運行的方法
這篇文章主要介紹了C#實現(xiàn)托盤程序并禁止多個應(yīng)用實例運行的方法,涉及C#中NotifyIcon控件的使用及設(shè)置標(biāo)志位控制程序只運行一個的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11C#中的ICustomFormatter及IFormatProvider接口用法揭秘
這篇文章主要介紹了C#中的ICustomFormatter及IFormatProvider接口用法揭秘,本文能過分析一段代碼得出一些研究結(jié)果,需要的朋友可以參考下2015-06-06C#實現(xiàn)網(wǎng)絡(luò)小程序的步驟詳解
經(jīng)常要檢測某些IP地址范圍段的計算機是否在線。有很多的方法,比如進(jìn)入到網(wǎng)關(guān)的交換機上去查詢、使用現(xiàn)成的工具或者編寫一個簡單的DOS腳本等等,這些都比較容易實現(xiàn)。本文將用C#來實現(xiàn),感興趣的可以了解一下2022-12-12