关于C#中"this"语句的简单问题
Simple question about "this" statement in C#
class first
{
internal string log = "";
public bool something(second obj)
{
if (obj.check(this) == true)
return true;
else
return false;
}
}
class second
{
public bool check(first obj)
{
if (obj.log == "yes")
return true;
else
return false;
}
}
所以在上面这段代码中有一个语句 obj.check(this)
,我想知道 this
指的是什么?我刚开始编程,我的教授并没有真正深入其中。
this
是指向正在调用该方法的 "current" 对象的指针。
The this keyword refers to the current instance of the class.
在您的情况下,它指的是 class 的当前实例,您正在检查当前 class 的实例(即 class first
的实例)包含记录值是否为 "yes"。
在 C# 中,您使用的对象是特定 class 的实例。
您可能会将 class 视为某物的蓝图。例如如何制造汽车的蓝图,但它不是汽车。一个对象是该蓝图的一个实例,例如每辆车都是 class Car
.
的一个对象
当您使用 this
时,您指的是您当前正在使用的对象。例如,当您和朋友在大厅时,系统会询问您谁在注册活动。你可以回复me
,每个人都知道谁在注册。在这种情况下 me
是 this
并告诉它指的是谁。
this 指针表示 class 或结构的当前实例。
所以class第一个"something"中的函数和下面一样
public bool something(second obj)
{
if (log == "yes")
return true;
else
return false;
}
class first
{
internal string log = "";
public bool something(second obj)
{
if (obj.check(this) == true)
return true;
else
return false;
}
}
class second
{
public bool check(first obj)
{
if (obj.log == "yes")
return true;
else
return false;
}
}
所以在上面这段代码中有一个语句 obj.check(this)
,我想知道 this
指的是什么?我刚开始编程,我的教授并没有真正深入其中。
this
是指向正在调用该方法的 "current" 对象的指针。
The this keyword refers to the current instance of the class.
在您的情况下,它指的是 class 的当前实例,您正在检查当前 class 的实例(即 class first
的实例)包含记录值是否为 "yes"。
在 C# 中,您使用的对象是特定 class 的实例。
您可能会将 class 视为某物的蓝图。例如如何制造汽车的蓝图,但它不是汽车。一个对象是该蓝图的一个实例,例如每辆车都是 class Car
.
当您使用 this
时,您指的是您当前正在使用的对象。例如,当您和朋友在大厅时,系统会询问您谁在注册活动。你可以回复me
,每个人都知道谁在注册。在这种情况下 me
是 this
并告诉它指的是谁。
this 指针表示 class 或结构的当前实例。
所以class第一个"something"中的函数和下面一样
public bool something(second obj)
{
if (log == "yes")
return true;
else
return false;
}