如何在每个类实例中使用引用类型属性来保存Java中的最新实例

时间:2013-11-26 07:05:54

标签: java

虽然在下面的Java代码中,类unSaved的每个实例中的TestRef属性如何保存最新的unSaved对象?

我试了一下,TestRef类的每个实例的unSaved属性都持有unSaved,它调用setUnSaved()设置它,但我想实现它保持最新的unSaved(这是最新的) TestRef对象)。

package com.test.ref;
public class TestRef
{
    private TestRef unSaved;
    private String name;

    public  TestRef(String name)
        {
           this.name = name;
        }

    public String getName(){
    return name;
    }

        public TestRef getUnSaved()
    {
         return unSaved;
    }

    public void setUnSaved(TestRef unSaved)
    {
    this.unSaved = unSaved;
    }

    public void save()
    {
    System.out.println("save Finished");
    }

    public static void main(String[] args)
    {
    TestRef unSaved = null;
    TestRef obj1 = new TestRef("obj1");
    unSaved = obj1;
    obj1.setUnSaved(unSaved);
    TestRef obj2 = new TestRef("obj2");
    unSaved = obj2;

               //the unSaved in the obj1 is not reference to the newest obj(that is obj2),How Can I achieve it? 
    System.out.println(obj1.unSaved.getName());
    System.out.println(unSaved.getName());          
    }

}

2 个答案:

答案 0 :(得分:1)

通过使用静态变量(类变量)解决了整个问题。为什么要在每个对象实例中保留对同一对象的引用。只需将此引用存储为类变量即可。要做到这一点,你必须做两件事:

  1. 使构造函数成为私有,以控制谁在创建这些对象。

  2. 将最新创建的实例保存在静态成员变量中(如果考虑多线程环境,可能会同步该方法)。

  3. 通过它,您始终保持对最新创建对象的引用。调整你的代码示例你应该有这样的东西:

    package com.test.ref;
    
    public class TestRef {
      private static TestRef unSaved;
    
      private String name;
    
      private TestRef(String name) {
        this.name = name;
        TestRef.unSaved = this;
      }
    
      public String getName() {
        return this.name;
      }
    
      public static TestRef getUnSaved() {
        return unSaved;
      }
    
      public void save() {
        System.out.println("save Finished");
      }
    
      public static void main(String[] args) {
        TestRef obj1 = new TestRef("obj1");
        TestRef obj2 = new TestRef("obj2");
        System.out.println(TestRef.unSaved.getName());
        System.out.println(obj1.getName());
        System.out.println(obj2.getName());
      }
    
    }
    

答案 1 :(得分:0)

此问题仅与根据您的要求设计解决方案有关。您可以找到许多不同的实现方法。

这是一个例子。但你应该知道这可能不是一个好的设计......

package com.test.ref;

import java.util.ArrayList;
import java.util.List;

public class TestRef {

    // TO ACCESS ALL INSTANCES OF TEST REF FOR UPDATE LATEST UNSAVED OBJECT REFENRECE
    private static List<TestRef> myObjList = new ArrayList<TestRef>();

    private TestRef unSaved;

    private String name;

    // GET OBJECT LIST TO CONSTRUCTOR
    public TestRef(String name, List<TestRef> myObjList) {
        this.name = name;

        // UPDATE ALL REFERENCES
        myObjList.add(this);
        for(TestRef myObj : myObjList)
        {
            myObj.setUnSaved(this);
        }
    }

    public String getName() {
        return name;
    }

    public TestRef getUnSaved() {
        return unSaved;
    }

    public void setUnSaved(TestRef unSaved) {
        this.unSaved = unSaved;
    }

    public void save() {
        System.out.println("save Finished");
    }

    public static void main(String[] args) {
        TestRef unSaved = null;

        TestRef obj1 = new TestRef("obj1", myObjList);

        unSaved = obj1;

        obj1.setUnSaved(unSaved);

        TestRef obj2 = new TestRef("obj2", myObjList);

        unSaved = obj2;

        // the unSaved in the obj1 is not reference to the newest obj(that is
        // obj2),How Can I achieve it?
        System.out.println(obj1.unSaved.getName());

        System.out.println(unSaved.getName());
    }
}

正如我所提到的,这不是完美的解决方案。您可以阅读Singelton或其他design patterns来了解更多信息。