Java是如何创建一个新对象的

Java 教程是为 JDK 8 编写的。本页中描述的示例和实践没有利用更高版本中引入的改进,并且可能使用不再可用的技术。
有关Java SE 9 和后续版本中更新的语言功能的摘要,请参阅Java 语言更改。
有关所有JDK 版本的新功能、增强功能以​​及已删除或不推荐使用的选项的信息,请参阅JDK 发行说明。

本文章翻译自:https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

1. 创建一个对象

正如你所知的那样,类为对象提供了大概的蓝图;

你从一个类中创建一个对象。下列大的语句来自CreateObjectDemo,在这个程序中,每一条语句都会创建一个对象,程序给每一个变量赋值。
(注释:代码框内所有等号左侧的代码均为粗体,可能未正确显示)

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

第一条语句创建了一个Point类的对象,随后的第二第三条语句为Rectangle类创建对象。

Each of these statements has three parts (discussed in detail below):
这些语句都有三个部分(下文将详细讨论);

  1. Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.

    声明:那些粗体大的代码都是对变量的声明,它们联系了变量名与变量类型的关系
    (意思是说Point orginOne= 这样的语句声明了originOne的类型,其类型属于Point类)

  2. Instantiation: The new keyword is a Java operator that creates the object.

    实例化:new这个关键字是Java的一种操作,通过这个操作创建了对象。

  3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

    初始化:跟在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 is null.

处于这种没有引用对象的变量,用如下图所示(变量名: 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.
这一陈述将在下一节中讨论。
(注释:如果有疑问请继续看下一节)

Initializing an Object

初始化一个对象

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:
执行该语句的结果如下图所示:

originOne now points to a Point object.

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。现在有两个对同一个点对象的引用——一个对象可以有多个对它的引用,如下图所示:

Now the rectangle's origin variable also points to the Point.

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 yvalues 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

posted @ 2018-11-22 19:20  ED_Reagan  阅读(708)  评论(0编辑  收藏  举报