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而编写的, 文中所描述的例子与实践并没有对后续版本的引入做出改进。

Creating Objects

创建一个项目

As you know, a class provides the blueprint for objects;

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

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,在这个程序中,每一条语句都会创建一个对象,程序给每一个变量赋值。

(注释:代码框内所有等号左侧的代码均为粗体,可能未正确显示)

  1. Point originOne = new Point(23, 94);
  2. Rectangle rectOne = new Rectangle(originOne, 100, 200);
  3. 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类创建对象。

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这个操作的后面的,是对构造器的调用,构造器初始化了新对象。

Declaring a Variable to Refer to an Object

声明一个引用的对象(引用数据类型)

Previously, you learned that to declare a variable, you write:

以往,根据你学到的声明变量的方法,你会这样写:

  1. 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:

你也可以声明一个引用数据类型(注释:前面的应该是基本数据类型)在同一行。例如

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

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操作符返回它对它所创建对象的引用。这个引用通常被分配给适当类型的变量。

  1. 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返回的引用不一定必须分配给变量。他也可以直接被使用。例如:

  1. 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这个类的一些代码:

  1. public class Point {
  2. public int x = 0;
  3. public int y = 0;
  4. //constructor
  5. public Point(int a, int b) {
  6. x = a;
  7. y = b;
  8. }
  9. }

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这两个值给所提到的两个参数:

  1. 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:

  1. public class Rectangle {
  2. public int width = 0;
  3. public int height = 0;
  4. public Point origin;
  5. // four constructors
  6. public Rectangle() {
  7. origin = new Point(0, 0);
  8. }
  9. public Rectangle(Point p) {
  10. origin = p;
  11. }
  12. public Rectangle(int w, int h) {
  13. origin = new Point(0, 0);
  14. width = w;
  15. height = h;
  16. }
  17. public Rectangle(Point p, int w, int h) {
  18. origin = p;
  19. width = w;
  20. height = h;
  21. }
  22. // a method for moving the rectangle
  23. public void move(int x, int y) {
  24. origin.x = x;
  25. origin.y = y;
  26. }
  27. // a method for computing the area of the rectangle
  28. public int getArea() {
  29. return width * height;
  30. }
  31. }

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.

如果一个类有多个构建器,它必须有不同的参数数量和类型。

注释:

例如这两个构建器:

  1. public Rectangle(int w, int h) {
  2. origin = new Point(0, 0);
  3. width = w;
  4. height = h;
  5. }
  6. public Rectangle(Point p, int w, int h) {
  7. origin = p;
  8. width = w;
  9. height = h;
  10. }

它们有不同类型的参数,参数的数量也不同。第一个构建器共有两个参数都是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类的参数大的构建器,并且还是需要两个参数的。

  1. 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 yvalues are initialized to 0:

  1. 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:

  1. 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

Java中如何创建一个新的对象的/Creating Objects/的更多相关文章

  1. 什么是不可变对象(immutable object)?Java 中怎么 创建一个不可变对象?

    不可变对象指对象一旦被创建,状态就不能再改变.任何修改都会创建一个新的对象,如 String.Integer 及其它包装类. 详情参见答案,一步一步指导你在 Java中创建一个不可变的类.

  2. Java中直接输出一个类的对象

    例如 package com.atguigu.java.fanshe; public class Person { String name; private int age; public Strin ...

  3. java 中,new一个新对象时,是先给成员变量赋上初值后 再来调用类中的构造函数的。

    今天学习时法现一个问题,我们定义了一个Test类,在主类中new了一个他的对象,发现:在新建对象中所有的成员变量是先给定了默认初值的:0,null或者false, 之后再调用的构造函数.(如果变量是由 ...

  4. JavaScript 如何从引用类型(Array 、 Object)创建一个新的对象

    数组的增删改 1.新增一项可以使用concat方法,它不会对原有数组进行改动,而是创建一个新数组 let a = [0, 1, 2] let b = a.concat([3]) console.log ...

  5. Java中如何创建一个确保唯一的名字,文件名

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客要讲的有,如何创建一个唯一的文件名,创建一个唯一的String字符串 为什么要创建唯一呢?再很多情况下 ...

  6. JS——数组中push对象,覆盖问题,每次都创建一个新的对象

    今天写运动框架时,发现将对象push进数组,后面的值会覆盖前面的值,最后输出的都是最后一次的值.其实这一切都是引用数据类型惹的祸.       如果你也有类似问题,可以继续看下去哦.       下面 ...

  7. JAVA中如何创建一个二维数组,然后给二维数组赋值!

    普通的赋值是:int[][] i = {{1,2,3},{2,3,4},{1,3,4}}; 如果是其他情况可以这样:比如: import java.util.* public class TT(){ ...

  8. Java中String创建原理深入分析

    创建String对象的常用方式: 1.  使用new关键字 String s1 = new String(“ab”);  // 2.  使用字符串常量直接赋值 String s2 = “abc”; 3 ...

  9. Eclipse中创建一个新的SpringBoot项目

    在Eclipse中创建一个新的spring Boot项目: 1. 首先在Eclipse中安装STS插件:在Eclipse主窗口中点击 Help -> Eclipse Marketplace... ...

随机推荐

  1. docker php容器中简单添加seaslog拓展

    最近有个项目用到了seaslog,因为之前调试php的容器已经搭好了,不想再通过dockerfile重新搭建了,搜了半天没有东西可以装,就仿照着安装redis拓展操作了一顿 1.wget http:/ ...

  2. [Sw] Swoole-4.2.9 可以尝试愉快应用 Swoole 协程

    大家知道 Swoole 提供了方便于服务器.网络编程的模式,简化了多进程编程. 这直接让 PHP 的运行很容易变成常驻内存的 Server 程序,执行效率上有了数倍的提升. 但是这一切还没有让人足够兴 ...

  3. jsTree通过AJAX从后台获取数据

    页面代码: <div id="MenuTree"></div> javascript代码: $(document).ready(function ($) { ...

  4. js中的“默默的失败”

    看阮一峰的js标准教程,看到了“默默的失败”觉得很形象也很无奈, 总结一下都有哪些地方会“默默的失败” 字符串内部的单个字符无法改变和增删,这些操作会默默地失败. var s = 'hello'; d ...

  5. 视频修复工具recover_mp4,视频录制一半掉电,如何查看已保存数据?

    在生产环境中,视频通常是一种重要的文件证据,但是,如果因为各种原因,导致视频在录制到一半过程中失败, 比如:监控到一半,录制设备掉电.虽然,掉电后的视频肯定找不到,但是,有时,长时间工作生产的视频通常 ...

  6. python day05笔记总结

    2019.4.2 S21 day05笔记总结 一.昨日内容回顾与补充 1.extend(列表独有功能) 循环添加到一个列表中 a.有列表users = ['张三',‘李四]   people = [' ...

  7. winfrom中pictureBox控件的部分使用方法

    一.后台属性 1.pictureBox1.Image显示图片 2.pictureBox1.ImageLocation存储和提取图片路径 二.面板属性 1.Picturebox控件SizeMode属性 ...

  8. 3D Math Keynote 3

    [3D Math Keynote 3] 1.球的表面积 Surface.球的体积 Volumn: 2.当物体旋转后,如果通过变换后的旧AABB来顶点来计算新的AABB顶点,则生成的新AABB可能比实际 ...

  9. MySql中order by和union all同时使用

    () UNION ALL () 两边的语句加上括号就可以了

  10. mysql链接服务器,update报错

    select * from Openquery(MySQL, 'SELECT * FROM official.sys_hospital') 执行更新语句: ; 报错,错误信息: 链接服务器" ...