Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改JAVA中复制对象的值_Java - Fatal编程技术网

更改JAVA中复制对象的值

更改JAVA中复制对象的值,java,Java,我知道这个问题已经被问了很多次了,但是我不知道我的代码中有什么问题 DateTry date1 = new DateTry("January", 3, 2004); DateTry date2 = new DateTry(date1); date1.setMonth("July"); System.out.println(date1.getDate()); System.out.println(date2.getDate()); public class DateTry { publ

我知道这个问题已经被问了很多次了,但是我不知道我的代码中有什么问题

DateTry date1 = new DateTry("January", 3, 2004);
DateTry date2 = new DateTry(date1);
date1.setMonth("July");
System.out.println(date1.getDate());
System.out.println(date2.getDate());

public class DateTry {

    public String month;
    public int day, year;

    public DateTry(String month, int day, int year) {
        this.month = month;
        this.day = day;
        this.year = year;
    }

    public DateTry (DateTry test) {
        this.month = test.month;
        this.day = test.day;
        this.year = test.year;
    }

    public String getDate() {
        return this.month + ", " + this.day + ", " + this.year;
    }
}
为什么第一行输出:“2004年7月3日” 第二行:“2004年1月3日”?我希望他们都能输出“2004年7月3日”

我想我必须改变这些路线:

this.month = test.month;
this.day = test.day;
this.year = test.year;
我看到了一些看起来像这样的答案,但对我来说并不适用:

this.month = new DateTry(test.month); etc...
为什么第一行输出“2004年7月3日”,第二行输出“2004年1月3日”?我希望他们都能输出“2004年7月3日”

因为
date1
date2
指的是两个不同的对象。您可以修改date1的状态:

date1.setMonth("July");
但是你没有修改date2的状态

假设你有两张纸。你在他们两个身上都写了同样的文字“hello world”。然后,在第一张纸上删除Hello并替换为再见。你在第二张上读到了什么?“你好,世界”,对吗?这里也一样

我希望他们都能输出“2004年7月3日”

然后还要修改date2的状态:

date2.setMonth("July");
如果希望这两个变量引用同一对象,则创建一个对象,并创建两个变量引用此唯一对象:

DateTry date1 = new DateTry("January", 3, 2004);
DateTry date2 = date1;

您只为date1设置了月份,还必须为date2设置月份。
另一种解决方案是在将date1设置为July之后创建date2实例。

您可以为String创建一个包装类来表示月份,并将月份的更改委托给它:

DateTry date1 = new DateTry(new Month("January"), 3, 2004);
DateTry date2 = new DateTry(date1);
date1.setMonth("July");
System.out.println(date1.getDate());
System.out.println(date2.getDate());

public class Month {
    private String month;

    public Month(String month) {
        this.month = month;
    }

    public String getMonth() {
        return this.month;
    }

    public void setMonth(String month) {
        this.month = month;
    }
}

public class DateTry {

    public Month month;
    public int day, year;

    public DateTry(Month month, int day, int year) {
        this.month = month;
        this.day = day;
        this.year = year;
    }

    public DateTry (DateTry test) {
        this.month = test.month;
        this.day = test.day;
        this.year = test.year;
    }

    public String getDate() {
        return this.month.getMonth() + ", " + this.day + ", " + this.year;
    }

    public void setMonth(String month) {
        this.month.setMonth(month);
    }
}

我想创建两个对象并使用相同的数据。这就是为什么我需要一个复制构造函数,对吗?根据定义,复制构造函数创建一个副本。如果制作文档的副本,然后修改原始文档,则副本不受影响。这就是制作副本的全部意义。欢迎使用堆栈溢出!看起来您需要学习使用调试器。请随便吃点。如果您以后仍然有问题,请随时回来提出更具体的问题。