java 理解Java中的深拷贝构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12379297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding the deep copy constructor in Java
提问by Dog
I have a main class called Bar that calls the class Foo, and I think I put in a deep constructor correctly
我有一个名为 Bar 的主类,它调用类 Foo,我想我正确地放入了一个深层构造函数
The purpose of a deep copy constructor is to copy the contents of one object to another object and changing the copied object shouldn't change the contents of the original, correct?
深拷贝构造函数的目的是将一个对象的内容复制到另一个对象,更改复制的对象不应更改原始内容,对吗?
My code does that, but I don't understand why when I set the original object variable, the copying object doesn't contain that set variable, it just contains the default constructor variable.
我的代码这样做,但我不明白为什么当我设置原始对象变量时,复制对象不包含该设置变量,它只包含默认构造函数变量。
public class Bar
{
public static void main(String[] args)
{
Foo object = new Foo();
object.setName1("qwertyuiop");
//the below line of code should copy object to object2?
Foo object2 = new Foo(object);
System.out.println(object.getName1());
//shouldn't the below line of code should output qwertyuiop since object2 is a copy of object? Why is it outputting the default constructor value Hello World?
System.out.println(object2.getName1());
//changes object2's name1 var to test if it changed object's var. it didn't, so my deep copy constructor is working
object2.setName1("TROLL");
System.out.println(object2.getName1());
System.out.println(object.getName1());
}
}
public class Foo
{
//instance variable(s)
private String name1;
public Foo()
{
System.out.println("Default Constructor called");
name1= "Hello World";
}
//deep copy constructor
public Foo(Foo deepCopyObject)
{
name1 = deepCopyObject.name1;
}
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
}
回答by duffymo
That's notdeep copy. Java is notC++. You are free to write a copy constructor that takes a Foo instance and initializes it with another Foo, but there's no language support to help you with the implementation. It's completely up to you.
那不是深拷贝。Java不是C++。您可以随意编写一个复制构造函数,它接受一个 Foo 实例并用另一个 Foo 对其进行初始化,但是没有语言支持来帮助您进行实现。这完全取决于你。
You also should know that Java doesn't require a copy constructor the same way C++ does. Java objects live on the heap. The thing you pass to a method is a reference to an object on the heap, not a copy of an object.
您还应该知道 Java 不像 C++ 那样需要复制构造函数。Java 对象存在于堆中。您传递给方法的内容是对堆上对象的引用,而不是对象的副本。
You can write a copy constructor, but it's up to you how it acts. You've got to be very careful:
您可以编写一个复制构造函数,但它的行为方式取决于您。你必须非常小心:
public class Foo {
private Map<String, Bar> barDictionary;
public Foo() {
this.barDictionary = new HashMap<String, Bar>();
}
public Foo(Foo f) {
// What happens here is up to you. I'd recommend making a deep copy in this case.
this.barDictionary = new HashMap<String, Bar>();
this.barDictionary.putAll(f.barDictionary); // Question: What about the Bar references? What happens to those?
}
}
回答by ILMTitan
Firstly, because String
is immutable, your Foo
doesn't really have any depth, so the difference between a deep copy and a shallow copy doesn't really exist. For non-immutable type fields, setting this.field = other.field
will make this
a shallow copy. You will need to make a deep copy of other.field
(recursively until you reach objects with only immutable or primitive fields).
首先,因为String
是不可变的,你Foo
真的没有任何深度,所以深拷贝和浅拷贝之间的区别并不真正存在。对于非不可变类型的字段,设置this.field = other.field
会做this
一个浅拷贝。您将需要制作一个深度副本other.field
(递归直到您到达仅具有不可变或原始字段的对象)。
Secondly, to answer the question in the comment in you code, it works correctly for me. System.out.println(object2.getName1());
does output qwertyuiop
.
其次,要回答您代码中注释中的问题,它对我来说是正确的。System.out.println(object2.getName1());
确实输出qwertyuiop
。