了解Java中的深层复制构造函数 [英] Understanding the deep copy constructor in Java

查看:67
本文介绍了了解Java中的深层复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Bar的主类,它调用Foo类,我想我正确地放入了一个深层构造函数

I have a main class called Bar that calls the class Foo, and I think I put in a deep constructor correctly

深层拷贝构造函数的目的是将一个对象的内容复制到另一个对象,并且更改复制的对象不应更改原始对象的内容,对吗?

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;
}
}


推荐答案

不是深层副本。 Java 不是 C ++。您可以自由地编写一个采用Foo实例并使用另一个Foo对其进行初始化的副本构造函数,但是没有语言支持可以帮助您实现。完全由您决定。

That's not deep copy. Java is not C++. 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 ++一样的复制构造函数。 Java对象位于堆上。传递给方法的内容是对堆上的对象的引用,而不是对对象的副本。

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.

您可以编写一个副本构造函数,但这取决于您如何操作。您必须非常小心:

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?
    }
}

这篇关于了解Java中的深层复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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