Can someone explain to me in detail the use of 

2019-02-27 21:51发布

问题:

I don't really understand the use of 'this' in Java. If someone could help me clarify I would really appreciate it.

On this website it says: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

"Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this."

and it gives the following example:

For example, the Point class was written like this

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}

but it could have been written like this:

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Yet, I still don't fully understand why x = a could have been written as this.x = x? Why isn't it this.x = a? Why is the x on the left side?

I'm sorry but I am very new to Java. I apologize for boring the experts.

回答1:

If some variable/argument with same name as object's property is defined, it "overlaps" the name of that property and one should use this.var_name.

So yes, it could be written as this.x = a, but is somewhat redundant.



回答2:

In the second example one of the arguments is named x. By referencing this.x, you mean the x field/property of the class the method is part of. It could read as: Point.x = argument x

this is being used to differentiate the two, making the meaning of the code clear.



回答3:

It isn't this.x = a because there isn't an 'a' in the second example. The point is that you can reuse the same variable name, which is less confusing :-|



回答4:

The idea is to make it very clear that you are providing values for x and yin your constructor.

Problem is now that due to the scoping rules that within the constructor x refers to the passed value and not the field x. Hence x = x results in the parameter being assigned its own value and the shadowed field untouched. This is usually not what is wanted.

Hence, a mechanism is needed to say "I need another x than the one immediately visible here". Here this refers to the current object - so this.x refers to a field in the current object, and super refers to the object this object extends so you can get to a field "up higher".



回答5:

this is a reference to the current object, so you access it like any other object - this.x is the x property of this. So x is the argument passed in, which you assign to this.x.

This is namespacing - the idea that a name for a variable only applies within a given block of code. In java, where you are working within a function belonging to the class, you are inside the namespace for that class, however, if you have another variable with the same name as an argument, it will take precedence, and you instead access the attribute via this.

this can also be used in other ways. For example, say I want to draw the current object to the screen in a fictional library, from within the class, I could do:

window.draw(this)

You can also call functions

this allows us to reference the object we are currently 'inside', so we can pass the current object as an argument. This is very useful. (No pun intended).



回答6:

"this" is a reference to the current object you are using. You use it when you have a name clash between a field and a parameter. Parameter takes precedence over fields.

No clash, no need for this:

public Point(int a, int b) {
    x = a;
    y = b;
}

But this will work, too:

public Point(int a, int b) {
    this.x = a;
    this.y = b;
}

Name clash, need to use "this":

public Point(int x, int y) {
    this.x = x;
    this.y = y;
}

If you did only

public Point(int x, int y) {
    x = x;
    y = y;
}

then you would just assign parameters with its own value, which effectively does nothing.

There are more usages of keyword "this".



回答7:

"This" is a hidden "argument" that gets passed for you so that the methods that operate on the object know which object exactly they are to operate on.

Now imagine you pass the argument of name "x" but the class does have that var name defined already. What happens ? Well, the name x that "belongs" to the object and the argument x are not the same data-object yet they share the name.

In order to disambiguate, you need to say explicitly "this.x", which tells the compiler that you're talking about the x that already belongs to "this" object. (That is, the current object you're trying to operate on.)



回答8:

In the second example, the arguments to the constructor are not a and b; they were changed to x and y, and this.x = x; means "assign this Point class instance's member variable x the value passed to the constructor as x".



回答9:

This really has to do with how the java compiler identifies variables by their name. Function (formaal) parameters names precede class member variables. In the first example the formal parameter names are a and b and they do not collide with the member variables x and y so writing

x = a;

is logical as x can only mean the member variable class Point.

In the second example x refers both to the formal parameter name and to the member variable. Writing x within the function body refers to the parameter so if you need some other way in order to refer to the member variable x. This is done by explicitly accessing a member via the 'this' keyword.