[英]Why must “this.x = x” be used when “return x” is allowed?
I have a little misunderstanding with a tutorial. 我对教程有一点误解。 Here is a cut from it:
这是一个切口:
public class Test {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Anyway the thing i cant understand is how to refer to id. 无论如何,我无法理解的是如何引用id。 For example i can see that in getId method i can directly acces previously defined id by just saying return id.
例如,我可以看到在getId方法中,我可以通过只返回id来直接访问先前定义的id。 But in setId method previously defined id is refered to as this.id and method parameter is id.
但是在setId方法中,先前定义的id被称为this.id,方法参数是id。
Now if there would be "return this.id" in get method then i would understand everything. 现在,如果在get方法中有“return this.id”,那么我会理解一切。 But at the moment i am confuzed.
但此刻我很困惑。 I assume that if i would return id in set method i would get the parameter back, not the class defined id.
我假设如果我将在set方法中返回id,我将返回参数,而不是类定义的id。 So in conclusion, class defined id can be acceced by just typing "id" unless there is a parameter passed with the same name?
总而言之,只要键入“id”就可以加入类定义的id,除非有一个带有相同名称的参数传递? That sounds kind of strange, what am i missing?
这听起来很奇怪,我错过了什么?
In Java, in normal conditions the this
inside of a class is optional. 在Java中,在正常情况下,
this
类里面是可选的。 any attribute can be refered with or without the this
. 任何属性都可以在有或没有
this
情况下引用。
When you have a parameter or a local variable with the same name, the ambiguity makes writing the this
, compulsory. 如果您有一个具有相同名称的参数或局部变量,那么歧义就是强制要求写
this
。
This is called " shadowing ". 这被称为“ 阴影 ”。 It is said that the local variable is shadowing the attribute.
据说局部变量正在遮蔽属性。
When you write id
, the most reasonable guess for Java is that you mean the most local reference, which is the parameter name rather than the attribute. 当你编写
id
,对Java的最合理的猜测是你指的是最本地的引用,它是参数名而不是属性。 To override this behavior, you have to clarify that you are willing to access this.id
, meaning the attribute, not the local variable. 要覆盖此行为,您必须澄清您是否愿意访问
this.id
,这意味着属性,而不是本地变量。
Hope that cleared things out! 希望把事情搞清楚!
this.id = id;
The problem is you also have a local variable of the same name. 问题是你还有一个同名的局部变量。 Because of this, you cannot access the class member directly, since
id
will refer to the local variable. 因此,您无法直接访问类成员,因为
id
将引用局部变量。 This is why this
is needed to access the class member variable here. 这就是为什么
this
是需要在这里访问类的成员变量。
You can rewrite as below, by changing the name, and now this
is not needed again. 你可以重写如下,通过更改名字,现在
this
是不是再次需要。
public void setId(int locID) {
id = locID;
}
You can put this.id in the getId method and it will still work. 你可以将this.id放在getId方法中,它仍然可以工作。 It's saying the same thing, actually.
实际上它说的是同样的事情。 This will compile and is perfectly valid:
这将编译并完全有效:
private int id;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
If you have created object from Test class and you trying to access that id
like this: 如果您已经从Test类创建了对象,并且您尝试像这样访问该
id
:
int test;
test=TestObject.id;
You will get error because id
is private so you must say 您将收到错误,因为
id
是私有的,所以您必须说
test=TestObject.getid();
Now getid()
will return for us the private id
we can call it because its public method 现在
getid()
将为我们返回我们可以调用它的私有id
,因为它的公共方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.