Documentation

The Java™ Tutorials
Hide TOC
Using the this Keyword使用this关键字
Trail: Learning the Java Language
Lesson: Classes and Objects
Section: More on Classes

Using the this Keyword使用this关键字

Within an instance method or a constructor, this is a reference to the current object在实例方法或构造函数中,this是对当前对象 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.您可以使用this从实例方法或构造函数中引用当前对象的任何成员。

Using this with a Fieldthis与字段一起使用

The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.使用this关键字最常见的原因是字段被方法或构造函数参数隐藏。

For example, the Point class was written like this例如,Point类是这样编写的

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;
    }
}

Each argument to the constructor shadows one of the object's fields构造函数的每个参数都会隐藏对象的一个字段 inside the constructor x is a local copy of the constructor's first argument.在构造函数x中是构造函数第一个参数的本地副本。To refer to the Point field x, the constructor must use this.x.若要引用Point字段x,构造函数必须使用this.x

Using this with a Constructorthis与构造函数一起使用

From within a constructor, you can also use the this keyword to call another constructor in the same class.在构造函数中,还可以使用this关键字调用同一类中的另一个构造函数。Doing so is called an explicit constructor invocation.这样做称为显式构造函数调用Here's another Rectangle class, with a different implementation from the one in the Objects section.这里是另一个Rectangle类,其实现与对象部分中的实现不同。

public class Rectangle {
    private int x, y;
    private int width, height;
        
    public Rectangle() {
this(0, 0, 1, 1);
    }
    public Rectangle(int width, int height) {
this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    ...
}

This class contains a set of constructors.此类包含一组构造函数。Each constructor initializes some or all of the rectangle's member variables.每个构造函数初始化矩形的部分或全部成员变量。The constructors provide a default value for any member variable whose initial value is not provided by an argument.构造函数为其初始值不是由参数提供的任何成员变量提供默认值。For example, the no-argument constructor creates a 1x1 Rectangle at coordinates 0,0.例如,无参数构造函数在坐标0,0处创建1x1RectangleThe two-argument constructor calls the four-argument constructor, passing in the width and height but always using the 0,0 coordinates.双参数构造函数调用四参数构造函数,传入宽度和高度,但始终使用0,0坐标。As before, the compiler determines which constructor to call, based on the number and the type of arguments.与前面一样,编译器根据参数的数量和类型确定要调用哪个构造函数。

If present, the invocation of another constructor must be the first line in the constructor.如果存在,则另一个构造函数的调用必须是构造函数中的第一行。


Previous page: Returning a Value from a Method
Next page: Controlling Access to Members of a Class