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发行说明。
Once you've created an object, you probably want to use it for something.一旦你创建了一个对象,你可能想用它做点什么。You may need to use the value of one of its fields, change one of its fields, or call one of its methods to perform an action.您可能需要使用其中一个字段的值、更改其中一个字段或调用其中一个方法来执行操作。
Object fields are accessed by their name. You must use a name that is unambiguous.对象字段通过其名称进行访问。您必须使用明确的名称。
You may use a simple name for a field within its own class.您可以为自己类中的字段使用简单名称。For example, we can add a statement within the 例如,我们可以在Rectangle
class that prints the width
and height
:Rectangle
类中添加一条语句,用于打印width
和height
:
System.out.println("Width and height are: " + width + ", " + height);
In this case, 在本例中,width
and height
are simple names.width
和height
是简单的名称。
Code that is outside the object's class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name, as in:对象类之外的代码必须使用对象引用或表达式,后跟点(.
)运算符,后跟简单字段名,如:
objectReference.fieldName
For example, the code in the CreateObjectDemo class is outside the code for the Rectangle class.例如,CreateObjectDemo类中的代码在Rectangle类的代码之外。So to refer to the origin, width, and height fields within the Rectangle object named rectOne, the CreateObjectDemo class must use the names rectOne.origin, rectOne.width, and rectOne.height, respectively.因此,要引用名为rectOne的矩形对象中的origin、width和height字段,CreateObjectDemo
类必须分别使用名称rectOne.origin
、rectOne.width
和rectOne.height
。The program uses two of these names to display the width and the height of rectOne:程序使用其中两个名称来显示rectOne的width和height:
System.out.println("Width of rectOne: " + rectOne.width); System.out.println("Height of rectOne: " + rectOne.height);
Attempting to use the simple names width and height from the code in the CreateObjectDemo class doesn't make sense those fields exist only within an object and results in a compiler error.试图使用CreateObjectDemo类中代码中的简单名称width和height没有意义;并导致编译器错误这些字段仅存在于对象。
Later, the program uses similar code to display information about rectTwo.之后,该程序使用类似的代码来显示有关rectTwo的信息。Objects of the same type have their own copy of the same instance fields.同一类型的对象有其自己的同一实例字段副本。Thus, each Rectangle object has fields named origin, width, and height.因此,每个Rectangle对象都有名为origin、width和height的字段。When you access an instance field through an object reference, you reference that particular object's field.通过对象引用访问实例字段时,引用该特定对象的字段。The two objects rectOne and rectTwo in the CreateObjectDemo program have different origin, width, and height fields.CreateObjectDemo程序中的两个对象rectOne和rectTwo具有不同的orgin、width
和height字段。
To access a field, you can use a named reference to an object, as in the previous examples, or you can use any expression that returns an object reference.要访问字段,可以使用对象的命名引用,如前面的示例所示,也可以使用任何返回对象引用的表达式。Recall that the new operator returns a reference to an object.回想一下,new运算符返回对对象的引用。So you could use the value returned from new to access a new object's fields:因此,您可以使用new返回的值访问新对象的字段:
int height = new Rectangle().height;
This statement creates a new Rectangle object and immediately gets its height.此语句创建一个新的Rectangle对象并立即获取其高度。In essence, the statement calculates the default height of a Rectangle.本质上,该语句计算Rectangle的默认高度。Note that after this statement has been executed, the program no longer has a reference to the created Rectangle, because the program never stored the reference anywhere.请注意,执行此语句后,程序不再具有对已创建Rectangle的引用,因为程序从未将引用存储在任何位置。The object is unreferenced, and its resources are free to be recycled by the Java Virtual Machine.对象是未引用的,其资源可以由Java虚拟机自由回收。
You also use an object reference to invoke an object's method.您还可以使用对象引用来调用对象的方法。You append the method's simple name to the object reference, with an intervening dot operator (.).使用中间的点运算符(.
)将方法的简单名称附加到对象引用。Also, you provide, within enclosing parentheses, any arguments to the method.此外,还可以在圆括号内提供该方法的任何参数。If the method does not require any arguments, use empty parentheses.如果该方法不需要任何参数,请使用空括号。
objectReference.methodName(argumentList);
or:
objectReference.methodName();
The Rectangle class has two methods: getArea() to compute the rectangle's area and move() to change the rectangle's origin.Rectangle类有两个方法:getArea()
用于计算矩形的面积,move()
用于更改矩形的原点。Here's the CreateObjectDemo code that invokes these two methods:下面是调用这两个方法的CreateObjectDemo代码:
System.out.println("Area of rectOne: " + rectOne.getArea()); ... rectTwo.move(40, 72);
The first statement invokes rectOne's 第一条语句调用rectOne的getArea()
method and displays the results.getArea()
方法并显示结果。The second line moves rectTwo because the move() method assigns new values to the object's origin.x and origin.y.第二行移动rectTwo,因为move()
方法为对象的origin.x和origin.y指定新值。
As with instance fields, objectReference must be a reference to an object.与实例字段一样,objectReference必须是对对象的引用。You can use a variable name, but you also can use any expression that returns an object reference.可以使用变量名,但也可以使用任何返回对象引用的表达式。The new operator returns an object reference, so you can use the value returned from new to invoke a new object's methods:new运算符返回对象引用,因此您可以使用new
返回的值调用新对象的方法:
new Rectangle(100, 50).getArea()
The expression new Rectangle(100, 50) returns an object reference that refers to a Rectangle object.表达式new Rectangle(100, 50)
返回引用矩形对象的对象引用。As shown, you can use the dot notation to invoke the new Rectangle's getArea() method to compute the area of the new rectangle.如图所示,可以使用点表示法调用新Rectangle的getArea()
方法来计算新矩形的面积。
Some methods, such as getArea(), return a value.某些方法,例如getArea()
,返回一个值。For methods that return a value, you can use the method invocation in expressions.对于返回值的方法,可以在表达式中使用方法调用。You can assign the return value to a variable, use it to make decisions, or control a loop.您可以将返回值分配给变量、使用它来做出决策或控制循环。This code assigns the value returned by getArea() to the variable 此代码将areaOfRectangle
:getArea()
返回的值分配给变量areaOfRectangle
:
int areaOfRectangle = new Rectangle(100, 50).getArea();
Remember, invoking a method on a particular object is the same as sending a message to that object.请记住,在特定对象上调用方法与向该对象发送消息相同。In this case, the object that getArea() is invoked on is the rectangle returned by the constructor.在本例中,调用getArea()
的对象是构造函数返回的矩形。
Some object-oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they are no longer needed.一些面向对象的语言要求您跟踪所创建的所有对象,并在不再需要它们时显式地销毁它们。Managing memory explicitly is tedious and error-prone.显式管理内存既繁琐又容易出错。The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you don't have to worry about destroying them.Java平台允许您创建任意数量的对象(当然,受系统处理能力的限制),并且您不必担心会破坏它们。The Java runtime environment deletes objects when it determines that they are no longer being used.Java运行时环境在确定不再使用对象时删除这些对象。This process is called garbage collection.这个过程称为垃圾收集。
An object is eligible for garbage collection when there are no more references to that object.当对象不再有引用时,该对象有资格进行垃圾收集。References that are held in a variable are usually dropped when the variable goes out of scope.变量中保存的引用通常在变量超出范围时删除。Or, you can explicitly drop an object reference by setting the variable to the special value null.或者,通过将变量设置为特殊值null
,可以显式删除对象引用。Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.记住,一个程序可以对同一个对象有多个引用;在对象符合垃圾收集条件之前,必须删除对该对象的所有引用。
The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer referenced.Java运行时环境有一个垃圾收集器,它定期释放不再被引用的对象所使用的内存。The garbage collector does its job automatically when it determines that the time is right.垃圾收集器在确定时间正确时自动执行其工作。