|
C# 参考之访问关键字:base、this(2) |
C# 参考之访问关键字:base、this(2)
// keywords_this.cs
thisthis 关键字引用类的当前实例。
以下是 this 的常用用途:
限定被相似的名称隐藏的成员 将对象作为参数传递到其他方法 声明索引器 示例:
综合示例。 // this 关键字
using System;class Employee{ private string _name; private int _age; private string[] _arr = new string[5];
public Employee(string name, int age) { // 使用this限定字段,name与age this._name = name; this._age = age; }
public string Name { get { return this._name; } }
public int Age { get { return this._age; } }
// 打印雇员资料 public void PrintEmployee() { // 将Employee对象作为参数传递到DoPrint方法 Print.DoPrint(this); }
// 声明索引器 public string this[int param] { get { return _arr[param]; } set { _arr[param] = value; } }
}class Print{ public static void DoPrint(Employee e) { Console.WriteLine("Name: {0}\nAge: {1}", e.Name, e.Age); }}
class TestApp{ static void Main() { Employee E = new Employee("Hunts", 21); E[0] = "Scott"; E[1] = "Leigh"; E[4] = "Kiwis"; E.PrintEmployee();
for(int i=0; i<5; i++) { Console.WriteLine("Friends Name: {0}", E[i]); }
Console.ReadLine(); }}
/**//*控制台输出:Name: HuntsAge: 21Friends Name: ScottFriends Name: LeighFriends Name: Friends Name: Friends Name: Kiwis*/注意点
由于静态成员函数存在于类一级,并且不是对象的一部分,因此没有 this 指针。在静态方法中引用 this 是错误的。 索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。 | |
|
|
|
|