標籤:style blog http color 使用 strong ar 2014
如有錯誤,歡迎指正。
1.代表當前類,在當前類中可使用this訪問當前類成員變數和方法(需要注意的是 靜態方法中不能使用this),也可用於參數傳遞,傳遞當前對象的引用。
下面貼代碼:
class Program { static void Main(string[] args) { thisClass testObj = new thisClass(); Console.ReadLine(); } } class thisClass { private string A { get; set; } public thisClass() { /*當前類this 訪問類中屬性A 靜態方法無法訪問A屬性*/ this.A = "Test String"; Console.WriteLine(this.TestFun("TestFun :")); } private string TestFun(string args) { return args + this.A; } }
2.聲明索引器
索引器:允許類和結構的執行個體按照與數組相同的方式進行索引,索引器類似與屬性,不同之處在於他們的訪問器採用參數,被稱為有參屬性,索引可以被重載,屬於執行個體成員,不能聲明為static。
下面貼代碼:
class Program { static void Main(string[] args) { indexClass intIndexClass = new indexClass(); intIndexClass[0] = new thisClass("intIndexClass 111"); intIndexClass[1] = new thisClass("intIndexClass 222"); indexClass stringIndexClass = new indexClass(); stringIndexClass["string1"] = new thisClass("stringIndexClass string1"); stringIndexClass["string2"] = new thisClass("stringIndexClass string2"); Console.ReadLine(); } } class indexClass { /*聲明屬性*/ private thisClass[] thisClassArr = new thisClass[10]; private Hashtable thisClassStrArr = new Hashtable(); /*建立索引器1 索引可以被重載,屬於執行個體成員,不能聲明為static*/ public thisClass this[int index] { get { return thisClassArr[index]; } set { this.thisClassArr[index] = value; } } /*建立索引器2*/ public thisClass this[string index] { get { return thisClassStrArr[index] as thisClass; } set { this.thisClassStrArr[index] = value; } } } class thisClass { private string A { get; set; } public thisClass(string str) { /*當前類this 訪問類中屬性A 靜態方法無法訪問A屬性*/ this.A = str; Console.WriteLine(this.TestFun("TestFun :")); } private string TestFun(string args) { return args + this.A; } }
我看了好像就這麼多,其他還有補充的沒?