The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases.这篇Java教程是为JDK 8而编写的, 文中所描述的例子与实践并无对后续版本的引入作出改进。java
As you know, a class provides the blueprint for objects;
正如你所知的那样,类为对象提供了大概的蓝图;express
you create an object from a class. Each of the following statements taken from the CreateObjectDemo
program creates an object and assigns it to a variable:
你从一个类中建立一个对象。下列大的语句来自CreateObjectDemo,在这个程序中,每一条语句都会建立一个对象,程序给每个变量赋值。
(注释:代码框内全部等号左侧的代码均为粗体,可能未正确显示)oracle
Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100);
The first line creates an object of the Point
class, and the second and third lines each create an object of the Rectangle
class.
第一条语句建立了一个Point类的对象,随后的第二第三条语句为Rectangle类建立对象。app
Each of these statements has three parts (discussed in detail below):
这些语句都有三个部分(下文将详细讨论);ide
Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.函数
声明:那些粗体大的代码都是对变量的声明,它们联系了变量名与变量类型的关系
(意思是说Point orginOne= 这样的语句声明了originOne的类型,其类型属于Point类)post
Instantiation: The new
keyword is a Java operator that creates the object.ui
实例化:new这个关键字是Java的一种操做,经过这个操做建立了对象。this
Initialization: The new
operator is followed by a call to a constructor, which initializes the new object.code
初始化:跟在new这个操做的后面的,是对构造器的调用,构造器初始化了新对象。
Previously, you learned that to declare a variable, you write:
以往,根据你学到的声明变量的方法,你会这样写:
type name;
This notifies the compiler that you will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable.
这告诉了编译器你将会使用一个name去引用一个type类型的变量。经过初始化这个变量,这个表述还为该变量保留了适当的内存。
You can also declare a reference variable on its own line. For example:
你也能够声明一个引用数据类型(注释:前面的应该是基本数据类型)在同一行。例如
Point originOne;
If you declare originOne
like this, its value will be undetermined until an object is actually created and assigned to it.
若是你这样声明originOne,那么在实际建立对象而且分配给它以前,它的值都是不肯定的。
Simply declaring a reference variable does not create an object.
经过简单的声明引用数据类型并不能建立一个对象。
For that, you need to use the new
operator, as described in the next section. You must assign an object to originOne
before you use it in your code. Otherwise, you will get a compiler error.
所以你须要使用new操做符,以下一节所述的那样。在使用以前必须将对象分配给originOne。不然你将会获得一个编译错误。
A variable in this state, which currently references no object, can be illustrated as follows (the variable name, originOne
, plus a reference pointing to nothing):
处于这种没有引用对象的变量,用以下图所示(变量名: originOne,加上一个指向nothing的引用)
The new
operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new
operator also invokes the object constructor.
new这个操做符实例化一个类经过为新对象分配内存并返回对该内存的引用。new这个操做符也调用了对象构建器(constructor)
(注释:new这个操做符在实例化一个类中其实作了三件事,前两件事能够看做一个总体。首先new这个操做符为要建立的对象分配内存,例如Point originOne = new Point(23, 94); 建立对象的过程当中,new这个操做符为对象originOne分配内存怕 /其实originOne只是一个句柄 / ,而后将内存的地址给了originOne。同时new这个操做符经过调用构建器,构建了一个对象。构建器时一种特殊的方法。)
Note:
The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.
注意:
短语“建立一个对象”和“实例化一个类”意思相同。当您建立一个对象时,也是在实例化一个类。
The new
operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate.
new操做符须要一个紧接在后的参数:一个对构建器的调用。构建器大的名字提供了要实例化类的名字。(注释:由于构建器的名称要求与类名称相同)
The new
operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like:
new操做符返回它对它所建立对象的引用。这个引用一般被分配给适当类型的变量。
Point originOne = new Point(23, 94);
The reference returned by the new
operator does not have to be assigned to a variable. It can also be used directly in an expression. For example:
这个被new返回的引用不必定必须分配给变量。他也能够直接被使用。例如:
int height = new Rectangle().height;
This statement will be discussed in the next section.
这一陈述将在下一节中讨论。
(注释:若是有疑问请继续看下一节)
Here's the code for the Point
class:
这是关于point这个类的一些代码:
public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } }
This class contains a single constructor. You can recognize a constructor because its declaration uses the same name as the class and it has no return type.
这个类包含了一个构建器(constructor)。你能够经过名称识别出构建器,由于它们与类的名称相同,并且它们没有返回类型。
The constructor in the Point
class takes two integer arguments, as declared by the code (int a, int b)
. The following statement provides 23 and 94 as values for those arguments:
point这个构建器包含有两个参数,这两个参数由代码(int a, int b),声明,下面的陈述提供了23和94这两个值给所提到的两个参数:
Point originOne = new Point(23, 94);
The result of executing this statement can be illustrated in the next figure:
执行该语句的结果以下图所示:
Here's the code for the Rectangle
class, which contains four constructors:
public class Rectangle { public int width = 0; public int height = 0; public Point origin; // four constructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; } public Rectangle(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } // a method for computing the area of the rectangle public int getArea() { return width * height; } }
Each constructor lets you provide initial values for the rectangle's origin, width, and height, using both primitive and reference types.
(对于上面的代码)每一个建造器须要你提供初始的的对于原点的高度和宽度的值。
If a class has multiple constructors, they must have different signatures. The Java compiler differentiates the constructors based on the number and the type of the arguments.
若是一个类有多个构建器,它必须有不一样的参数数量和类型。
注释:
例如这两个构建器:
public Rectangle(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; }
它们有不一样类型的参数,参数的数量也不一样。第一个构建器共有两个参数都是int类型。对于第二个构建器有三个参数其中第一个为Point类型, 其他的均为int型。
注释结束
When the Java compiler encounters the following code, it knows to call the constructor in the Rectangle
class that requires a Point
argument followed by two integer arguments:
当编译器遇到如下代码时,它知道调用在Rectangle类中须要Point类的参数大的构建器,而且仍是须要两个参数的。
Rectangle rectOne = new Rectangle(originOne, 100, 200);
This calls one of Rectangle
's constructors that initializes origin
to originOne
. Also, the constructor sets width
to 100 and height
to 200. Now there are two references to the same Point object
—an object can have multiple references to it, as shown in the next figure:
这将调用将原点初始化为originOne的矩形构造函数,此外,构造函数将宽度设置为100,高度设置为200。如今有两个对同一个点对象的引用——一个对象能够有多个对它的引用,以下图所示:
The following line of code calls the Rectangle
constructor that requires two integer arguments, which provide the initial values for width
and height
. If you inspect the code within the constructor, you will see that it creates a new Point
object whose x
and y
values are initialized to 0:
Rectangle rectTwo = new Rectangle(50, 100);
The Rectangle
constructor used in the following statement doesn't take any arguments, so it's called a no-argument constructor:
Rectangle rect = new Rectangle();
All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object
constructor if the class has no other parent. If the parent has no constructor (Object
does have one), the compiler will reject the program.
END