Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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中日期与getter和setter的易变性_Java_Reference_Mutable - Fatal编程技术网

java中日期与getter和setter的易变性

java中日期与getter和setter的易变性,java,reference,mutable,Java,Reference,Mutable,因为日期在java中是可变的,所以它们的getters setter最好直接作为 public void setDate(Date date){ this.date = date; } public Date getDate(){ return this.date; } 所以我在Ideone上写了一个代码来验证这个 /* package whatever; // don't place package name! */ import java.util.*; import ja

因为日期在java中是可变的,所以它们的getters setter最好直接作为

public void setDate(Date date){
   this.date = date;
}

public Date getDate(){
   return this.date;
}
所以我在Ideone上写了一个代码来验证这个

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    private class Test{
        private Date date;

        public void setDate(Date date){
            this.date = date;
        }

        public Date getDate(){
            return this.date;
        }
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        Ideone abc = new Ideone();
        abc.functionTest();

    }

    public void functionTest(){
        Date tempDate = new Date(1403685556000L);
        Test test = new Test();
        test.setDate(tempDate);
        tempDate = new Date(1435221556000L);
        Date abc = test.getDate();
        Long def = abc.getTime();
        System.out.println("Date 1:"+def.toString());
        test.setDate(tempDate);
        def = abc.getTime();
        System.out.println("Date 2:"+def.toString());
    }
}
我得到以下结果作为输出

Date 1:1403685556000
Date 2:1403685556000
这些是原始日期,而不是更改的日期。 这与java中日期对象的可变性所预期的结果相反

为什么我会有这种意想不到的行为


以下是

您必须稍后设置第二个
tempDate

您正在使用
tempDate=new Date(143521156000L)设置日期的新引用(然后
test.setDate(tempDate);

因此,您的“原始”日期不会更改,因为它不指向新的引用

    Date tempDate = new Date(1403685556000L);    // first reference.
    Test test = new Test();                  
    test.setDate(tempDate);                      
    tempDate = new Date(1435221556000L);         // second reference.
    Date abc = test.getDate();                   // you take the first reference since you do not set "tempDate" in Test before.
    Long def = abc.getTime();
    System.out.println("Date 1:"+def.toString()); // you print the first reference.
    test.setDate(tempDate);                       // you set the second reference and overwrite the first one.
    def = abc.getTime();                          // but "abc" still points on first reference.
    System.out.println("Date 2:"+def.toString());
这是指针:您的
abc
变量指向
Date
的第一个实例(由
newdate(1403685556000L)
声明)。如果通过设置新实例更改
Test.date
的值,
abc
仍将指向上一个指针


但是如果您使用
date
实例方法更改
Test.date
值,您将不会更改
Test.date
的引用,并且您的
abc
变量将反映这些更改。

这里有问题吗?您的问题是什么?是的,为什么我会遇到这种意外的行为您从未更改过您的
日期
,您刚刚创建了一个新的日期。您正在为日期设置一个新的引用。因此,合乎逻辑的是,您的“原始”日期没有更改,因为它没有指向新的引用。但第二次,在行内test.setDate(tempDate)我将tempDate引用到test.Date,但def在最后一次System.println中没有更新