学习this关键字
在学习MSDN的过程中加入一点自己的理解:
先学习类实例使用this
以下是 this 的常用用途:
-
限定被相似的名称隐藏的成员,例如:
public Employee(string name, string alias)
{
this.name = name;//此处的this.name即代表字段name,而非参数name
this.alias = alias;
} -
声明索引器,例如:public int this [int param]
{
get { return array[param]; }
set { array[param] = value; }
}/*这个之后单独开一页学习下索引器,索引器可以看做是一种带有参数的特殊属性,而且索引器的名称必须是关键字this还是很简单的。*/ -
将对象作为参数传递到其他方法,例如:
CalcTax(this);下面引用MSDN的官方示例并添加一些注释:
// keywords_this.cs
// this example
using System;
class Employee
{
private string name;
private string alias;
private decimal salary = 3000.00m;//此处的m不能省略否则编译器会视为double类型
// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name; //此处this.name非参数而是字段name
this.alias = alias;//此处this.alias非参数而是字段alais
}
// Printing method:
public void printEmployee()
{
Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
// Passing the object to the CalcTax method by using this:
Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));/*此处的this即代表Employee类实例,
在下面的Main函数中 Tax.CalcTax(this))实为调用Tax.CalcTax(E1));*/
}
public decimal Salary
{
get { return salary; }
}
}
class Tax
{
public static decimal CalcTax(Employee E)//将对象作为参数传递到CalcTax方法中
{
return 0.08m * E.Salary;//此处使用该对象E的Salary字段
}
}
class MainClass
{
static void Main()
{
// Create objects:
Employee E1 = new Employee("Mingda Pan", "mpan");
// Display results:
E1.printEmployee();
}
}
public decimal Salary
{
get { return salary; }
}
}
class Tax
{
public static decimal CalcTax(Employee E)//将对象作为参数传递到CalcTax方法中
{
return 0.08m * E.Salary;//此处使用该对象E的Salary字段
}
}
class MainClass
{
static void Main()
{
// Create objects:
Employee E1 = new Employee("Mingda Pan", "mpan");
// Display results:
E1.printEmployee();
}
}
同时当this用于构造函数事,它总是检查this的实参表匹配情况,先执行与其实参表最匹配的一个构造函数
使用this的构造函数形式如下:
【访问修饰符】 【类名】(『形式参数表』):this(『实际参数表』)
{
【语句块】
}
例如:
using System;
namespace test1
{
class test
{
//下面我声明两个参数数目和类型不同的构造函数
public test(string str1, int num)
{
Console.WriteLine("use test(string str1, int num) Constructor");
}
public test(int num):this("haha",2)//这个构造函数我用this声明
{
{
Console.WriteLine("use test( int num) Constructor");
}
}
//现在我们调用测试
public static void Main()
{
test contcorthis = new test(10);// 此时this的实参test(string str1, int num)与匹配
namespace test1
{
class test
{
//下面我声明两个参数数目和类型不同的构造函数
public test(string str1, int num)
{
Console.WriteLine("use test(string str1, int num) Constructor");
}
public test(int num):this("haha",2)//这个构造函数我用this声明
{
{
Console.WriteLine("use test( int num) Constructor");
}
}
//现在我们调用测试
public static void Main()
{
test contcorthis = new test(10);// 此时this的实参test(string str1, int num)与匹配
//应该先调用该构造函数
Console.ReadKey();
}
}
}
Console.ReadKey();
}
}
}
它的输出结果如下:
use test(string str1, int num) Constructor
use test( int num) Constructor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?