通过复制构造函数复制对象是否获得与原始对象相同的实例变量? [英] Does copy of an object through copy constructor get the same instance variable as the original object?

查看:127
本文介绍了通过复制构造函数复制对象是否获得与原始对象相同的实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有对象实例变量的对象的副本是否与原始对象获得相同的实例变量?如果是这样,我想知道原始和复制对象是否引用相同的实例变量。

Does a copy of an object with object instance variables get the same instance variable as the original object? if so, I was wondering if the original and copy objects are referencing to the same instance variables.

推荐答案

与C ++不同,Java确实如此不自动提供复制构造函数。因此,对于复制构造函数的行为没有任何一般性的答案,因为Java对它们的行为没有任何限制。

Unlike C++, Java does not provide copy constructors automatically. There is therefore no general answer to any question about the behavior of copy constructors, as Java places no restrictions on their behavior.

然而,每个对象,无论如何初始化,都有它的自己的实例变量。这些不与任何其他对象共享;它们可以与另一个对象相同,只有在保持相同值的意义上。原始类型的变量(例如 int )完全相互独立。引用类型的任何两个变量都可以包含引用同一个对象的值,但前提是它们的声明类型都与该对象的类兼容。复制构造函数可以轻松地创建这样的效果,但绝不需要这样做。

Nevertheless, every object, however initialized, has its own instance variables. These are not shared with any other object; they can be "the same" as another object's only in the sense of holding the same value. Variables of primitive type such as int are wholly independent of each other. Any two variables of reference type may hold values that refer to the same object, however, provided that their declared types are both compatible with the class of that object. A copy constructor could easily create such an effect, but in no way does one need to do so.

这篇关于通过复制构造函数复制对象是否获得与原始对象相同的实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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