简体   繁体   English

为什么在允许“返回x”时必须使用“this.x = x”?

[英]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.

相关问题 为什么在“if x then return”之后很少使用“else”? - Why is “else” rarely used after “if x then return”? 在构造对象时是否有在java中键入this.x = x的快捷方式? - Is there a shortcut for typing this.x = x in java while constructing an object? x + = ++ x相当于x = 2x + 1:为什么? - x += ++x equivalent to x = 2x+1 : Why? 什么时候以及为什么bufferedImage.getRGB(x,y)返回-1? - When and why would bufferedImage.getRGB(x, y) return -1? 为什么getClass返回一个Class <?延伸| X |>? - Why does getClass return a Class<? extends |X|>? 使用webdrivermanager-xxxjar时不下载/初始化浏览器驱动程序(无Maven) - Not download/init browser driver when used webdrivermanager-x.x.x.jar (without Maven) 为什么x ==(x = y)与(x = y)== x不相同? - Why is x == (x = y) not the same as (x = y) == x? x必须为&lt;bitmap.width() - x must be < bitmap.width() 为什么必须在paintComponent()中初始化x和y坐标? - Why must x and y coordinates be initialized inside paintComponent()? 为什么在使用 chrome 版本 91.x 和 selenium/chromedriver 无头模式时选项“--explicitly-allowed-ports”不起作用? - Why is option “--explicitly-allowed-ports” not working when using chrome version 91.x and selenium/chromedriver headless mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM