The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Within an instance method or a constructor, 在实例方法或构造函数中,this
is a reference to the current objectthis
是对当前对象 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
从实例方法或构造函数中引用当前对象的任何成员。
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 thisPoint
类是这样编写的
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
。
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 例如,无参数构造函数在坐标0,0处创建1x1Rectangle
at coordinates 0,0.Rectangle
。The 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.如果存在,则另一个构造函数的调用必须是构造函数中的第一行。