创建新对象时会发生什么? [英] What happens when you create a new object?

查看:55
本文介绍了创建新对象时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,那么当你这样做时会发生什么.

Ok, so what happens when you do this.

A a1=new A();

A a2=new A();

A a3=new A();

我上传了两张关于我想象中的照片.你能告诉我哪张照片是真的吗?

I upload two pictures on how I imagine it being like. Can you tell me what picture is true?

第一张图:

第二张图:

我一直认为第一张图是真的,但现在我真的不知道了,我怀疑第二张图是真的.

I always thought the first picture is true, but now I don't really know, and I suspect the second to be true.

另外,你能向我解释一下每一方是做什么的吗?比如,A a1"有什么作用,new A()"有什么作用?

Also, can you explain to me what each side does? Like, what does "A a1" do and what does "new A()" do?

谢谢.

推荐答案

new A() 将调用类 A 的无参数构造函数并创建一个新的内存对象.

new A() will call the no param constructor of class A and will create a new memory object.

A a1=new A();  // new memory object created

A a2=new A(); // new memory object created

A a3=new A(); // new memory object created

正在创建三个不同的对象,所以第二张图片是正确的.

There are three different objects that are getting created, so the SECOND picture is true.

为了使第一张图片为真,引用的声明应该是这样的:

For the FIRST picture to be true, the declaration of references should be something like this:

A a1=new A(); // new memory object created

A a2=a1; // a2 points to the same memory object as a1 does

A a3=a1; // a3 points to the same memory object as a1 does

这里只创建了一个对象(new 只使用一次),但所有三个引用都指向它.

Here only one object(new used only once) is created but all the three references point to it.

这篇关于创建新对象时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆