对“这个”感到困惑Java 中的运算符
尽管我试图理解为什么需要“this”,但我对其目的感到非常困惑。例如,我编写了以下代码:
public static void main (String args[])
{
SandboxClass1 temp = new SandboxClass1(1,2,3);
System.out.println(temp.getX());
System.out.println(temp.getY());
System.out.println(temp.getZ());
System.out.println("----------------------------");
SandboxClass1 temp2 = new SandboxClass1(4,5,6);
System.out.println(temp2.getX());
System.out.println(temp2.getY());
System.out.println(temp2.getZ());
}
public class SandboxClass1
{
private int x = 1;
private int y = 1;
private int z = 0;
public SandboxClass1(int x, int y, int zz)
{
this.x = x;
this.y = y;
z = zz;
}
public int getX()
{
return(this.x);
}
public int getY()
{
return(this.y);
}
public int getZ()
{
return(this.z);
}
}
为什么我需要编写“this.z = zz”
当我也可以写“z = zz”时?
Though I'm trying to understand why "this" is needed, I'm very confused about its purpose. For instance, I coded the following:
public static void main (String args[])
{
SandboxClass1 temp = new SandboxClass1(1,2,3);
System.out.println(temp.getX());
System.out.println(temp.getY());
System.out.println(temp.getZ());
System.out.println("----------------------------");
SandboxClass1 temp2 = new SandboxClass1(4,5,6);
System.out.println(temp2.getX());
System.out.println(temp2.getY());
System.out.println(temp2.getZ());
}
public class SandboxClass1
{
private int x = 1;
private int y = 1;
private int z = 0;
public SandboxClass1(int x, int y, int zz)
{
this.x = x;
this.y = y;
z = zz;
}
public int getX()
{
return(this.x);
}
public int getY()
{
return(this.y);
}
public int getZ()
{
return(this.z);
}
}
Why do I need to code "this.z = zz"
when I could just as well write, "z = zz"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在这种情况下,你不会。仅当您必须消除歧义时才需要它,例如参数和实例变量共享名称时。
有些人喜欢使用“this”来消除概念上的歧义,并明确声明代码引用实例变量。
(顺便说一句,返回值周围的括号是不必要的,而且有点吵,IMO。)
You don't, in this case. It's only required when you must eliminate ambiguity, like when parameters and instance variables share a name.
Some people prefer to use "this" to remove conceptual ambiguity and explicitly state that the code references an instance variable.
(On a side note, the parentheses around the return values are unnecessary and a bit noisy, IMO.)
在您的 SandboxClass1 构造函数中,您有两对变量,每个变量都称为 x 和 y。有在对象本身上声明的 x 和 y(“private int x = 1”),以及作为构造函数参数的单独 x 和 y(“int x”)。
局部(参数)变量遮蔽类变量。因此,如果在构造函数中您刚刚进行了
赋值,则不会产生任何效果。
关键字
this
是对调用该方法/构造函数的对象的引用。在语句中,您使用它在类级别分配给其他 x 。通过限定名称,您可以区分它们。
没有必要将
this
与 z/zz 赋值一起使用,因为它们具有不同的名称。在 getX/Y/Z 方法中也没有必要,因为这些方法中没有局部变量隐藏相关的类变量。不过这并没有什么害处。
In your SandboxClass1 constructor, you have two pairs of variables each called x and y. There's the x and y declared on the object itself ("private int x = 1"), and the separate x and y that are parameters to the constructor ("int x").
The local (parameter) variable shadows the class variable. So if in the constructor you just did
the assignment would have no effect.
The keyword
this
is a reference to the object that the method/constructor was called on. In the statementyou're using it to assign to the other x at class level. By qualifying the name, you can tell them apart.
It's not necessary to use
this
with the z/zz assignment because they have different names.It's also not necessary in the getX/Y/Z methods because there are no local variables in those methods shadowing the relevant class variables. It does no harm though.
在
SandboxClass1
构造函数中,两个参数(x
和y
)隐藏了类变量,因为它们具有相同的名称。如果您想在 code>SandboxClass1 构造函数中将类变量 x 分配给任何值,则必须使用this.x
来解决它,以告诉编译器“我想分配名为x,而不是名为 x 的方法范围变量”。这同样适用于y。由于参数
z
不会隐藏名为zz
的类作用域变量,因此您无需告诉编译器 zz 变量的作用域,因此类作用域 zz 是唯一可识别的变量,因此这就是被分配的变量。In the
SandboxClass1
constructor two of the parameters (x
andy
) hide class variables because they are the same name. If you want to assign the class variable x to any value while in the code>SandboxClass1 constructor, you must address it usingthis.x
to tell the compiler that "I want to assign the class scope variable named x, and not the method scope variable named x". The same applies to y.Since the parameter
z
does not hide the class scope variable namedzz
you do not need to tell the compiler the scope of the zz variable, the class scope zz is the only recognized variable so that is the one that gets assigned.它具有相同的效果。如果存在覆盖类字段的局部变量,则需要
this
;然后你得到局部变量而不是类字段。另一个优点是您可以更好地指示变量。如果有
this
;这是一个领域;否则为局部变量。It has the same effect.
this
is needed if there is a local variable which overrides a field of the class; then you get the local variable and not the class field.An additional advantage you can indicate the variables better. If there is a
this
; it's a field; local variable otherwise.关键字 this 用于引用类中的属性。创建关键字 this 是为了区分类属性和方法参数。像这样:
当你使用关键字 this 时,它引用类中的 name 变量,这里有一个你不需要使用 this 的例子:
现在看到只有 1 个名为 name 的变量,并且只有一个名为 myName 的变量。在另一个示例中,有 2 个名为 name 的变量。一种是类属性,一种是方法参数。
the keyword this is used to refer to an attribute that is in the class. The keyword this was created to distinguish between class attributes and method parameters. like this:
when you use the keyword this it refers to the name variable inside the class heres an example where you don't need to use this:
see now there is only 1 variable named name and there is only one variable named myName. In the other example there was 2 variables named name. One was a class attribute and one was a method parameter.
“this”运算符只是细化属于您正在使用的类的属性/字段。例如,当您有两个同名变量时,它很有用:
'this' operator just refines that property/field belongs to class you're working in. It's useful whe you have, for example, two variables with the same name:
与 Objective-C 不同,当方法或变量是本地的,并且没有其他同名的冲突使用时,“this”是可选的。
不过,它在名称冲突的情况下会派上用场,例如在设置实例变量的方法中,例如
void setOnionCount(int onionCount)
,您希望使用“onionCount”作为形式参数,但仍然将“onionCount”作为实例变量名称。在这种情况下,你可以这样做this.onionCount = onionCount;
并且每个人都很高兴(我想除了 Peanut Gallery 中那些反对这种技术的人)。当然,当您需要将对当前对象的引用传递给其他类时,“this”也是绝对必要的。
Unlike, say, Objective-C, "this" is optional when the method or variable is local, and there is no other conflicting use of the same name.
It comes in handy in conflicting-name cases, though, such as in methods that set instance variables such as
void setOnionCount(int onionCount)
where you would like to use "onionCount" for the formal parameter but still have "onionCount" as the instance variable name. In such a case you can dothis.onionCount = onionCount;
and everyone is happy (except, I suppose, those in the Peanut Gallery who'll object to this technique)."this" is also absolutely necessary in cases where you need to pass a reference to the current object to some other class, of course.
嘿,这是用来提供调用对象的引用。也就是说,假设你的类是框,那么如果你想提供它的对象,那么你可以使用
this
关键字在类框内提供它。在这种情况下,如果您的对象是 ob (即
Box ob = new Box(1)
),则它将传递给其自身的引用。注意:您不能在类之外使用此关键字。如果你使用它,那么它将提供另一个类的引用。
有关此关键字的完整详细信息,请参阅以下链接
http://download.oracle.com/javase/tutorial/java/javaOO /thiskey.html
hey this is use to provide reference of invoking object. That i.e say suppose ur class is box then if you want to provide it's object then you can provide it within the class box using
this
keyword.here in this case if your object is ob (i.e
Box ob = new Box(1)
) then the reference it will be passed to the itself.Note: you cannot use this keyword outside of the class. If you use so then it will give reference of another class.
for complete detail on this keyword refer following link
http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html