Java中如何建立一個新的對象的/Creating Objects/

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

Creating Objects

建立一個項目

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

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

    聲明:那些粗體大的代碼都是對變量的聲明,它們聯繫了變量名與變量類型的關係
    (意思是說Point orginOne= 這樣的語句聲明瞭originOne的類型,其類型屬於Point類)post

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

    實例化:new這個關鍵字是Java的一種操做,經過這個操做建立了對象。this

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

    初始化:跟在new這個操做的後面的,是對構造器的調用,構造器初始化了新對象。

Declaring a Variable to Refer to an Object

聲明一個引用的對象(引用數據類型)

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的引用)

Instantiating a Class

實例化一個類

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

相關文章
相關標籤/搜索