一个对象(如Obj)初始化后设置不同的赋值过程,目前想记录每次对象重新赋值之前的
记录,如何实现?以下arrayDataList.add(调用其他方法返回对象);中返回对象就是Obj,
arrayDataObj 中存储的就是每次Obj的信息,调试过程中发现arrayDataObj 的结果为最后一次
调用返回的Obj的内容,而且所有的都一样,因为指向了同一个引用ID,帮忙看看clone方法
是否正确?如何修改,才能使拷贝生效,而且是对象的拷贝不是引用的拷贝,谢谢!
===========================================
                ArrayList arrayDataList = new ArrayList();
for (int j = 0; j < arrayValues.length; j++) {
                            
                    arrayDataList.add(调用其他方法返回对象);
             CloneHandler testCone = new CloneHandler();
             testCone.setCloneOjb(arrayDataList.get(0));            
             CloneHandler resultCone = (CloneHandler) testCone.clone();
                    arrayDataObj[j] = resultCone.getCloneOjb();
                 paraType_ =  arrayDataList.get(0).getClass();
                }===========================================
重写的clone方法:
public class CloneHandler  implements Cloneable{ private Object aObj;

/**
 * Clone Object According to input Object
 * @return Object result of clone  
 * @throws CloneNotSupportedException 
 */
public Object clone(){    CloneHandler o = null;
   try{
   o = (CloneHandler)super.clone();         
   }catch(CloneNotSupportedException e){
       e.printStackTrace(); 
   }
   return o; 
}

/**
 * Set Object
 * @param obj Object to set  
 */
public void setCloneOjb(Object obj){
aObj = obj;
}

/**
 * Get Object
 * @return Object
 */
public Object getCloneOjb(){
   return aObj; 
}
===========================================

解决方案 »

  1.   

    你可以在你要返回的类中实现Cloneable接口,然后再内中实现你的clone方法;
    public Object clone()
    {
      try
      {
        yourClass cloned=(yourClass)super.clone();
        //添加需要自己clone的东西;
        return cloned;
       }catch(ClonedNotSupportedException e)
      {
        return null;
        }
    }然后你在返回此类的对象的时候候,即可调用clone方法获得一个拷贝,而不是一个引用的拷贝
      

  2.   

    问题关键点:就是想保存对象变更轨迹
    如:对象Obj = [中国,北京,010]
    第二次对象赋值Obj = [中国,上海,021]
    第三次对象赋值Obj = [中国,西安,029]Obj每次赋值过程都是针对同一个内存地址进行引用,
    想把每次赋值过程产生一个拷贝对象,存放起来。
      

  3.   

    huyc_fly() :
    在调用的类中添加clone()和单独定义的clone()有什么区别呢?
    “   //添加需要自己clone的东西;”不是需要通过方法来产生的吗?
    没看明白~
      

  4.   

    你这里是浅拷贝对象里面的属性还是指向同一个地址最后当然是同一个了要深拷贝,必须要要拷贝的对象组成的对象网中对象都实现clone接口
    调用其他方法返回对象 ,这个对象就要实现clone接口
      

  5.   

    import java.util.*;public class DeepClone {
        public static void main(String[] args) {
            List dataList = new ArrayList();
            Address add = new Address("中国","上海","021");
            for(int i = 0 ;i < 10;i++ ) {
                CloneHandler testCone = new CloneHandler();
                testCone.setCloneOjb(add);
                CloneHandler resultCone  = (CloneHandler)testCone.clone();
                dataList.add(resultCone.getCloneOjb());
                add.setAreaNum(add.getAreaNum()+i);
            }
            
            Iterator it = dataList.iterator() ;
            while(it.hasNext()) {
                System.out.println(  ((Address)it.next()).toString());
            }
        }
    }class Address implements Cloneable {
        private String country;    private String city;    private String areaNum;
        
        
        public Address() {
            super();
        }
        public Address(String country, String city, String areaNum) {
            super();
            this.country = country;
            this.city = city;
            this.areaNum = areaNum;
        }
        
        public Object clone() {
            try {
                return super.clone();
            } catch (Exception e) {
                return null;
            }
        }    public String getCity() {
            return city;
        }    public void setCity(String city) {
            this.city = city;
        }    public String getCountry() {
            return country;
        }    public void setCountry(String country) {
            this.country = country;
        }    public String getAreaNum() {
            return areaNum;
        }    public void setAreaNum(String areaNum) {
            this.areaNum = areaNum;
        }
        
        public String toString() {
            return "["+this.country+" , "+this.city+" , "+this.areaNum+"]" ;
        }
    }////////////////////////////////////
    class CloneHandler  implements Cloneable{    private Object aObj;
        
        /**
         * Clone Object According to input Object
         * @return Object result of clone  
         * @throws CloneNotSupportedException 
         */
        public Object clone(){       CloneHandler o = null;
           try{
               o = (CloneHandler)super.clone();
               o.setCloneOjb(((Address)this.aObj).clone()); //拷贝属性
               
           }catch(CloneNotSupportedException e){
               e.printStackTrace(); 
           }
           return o; 
        }
        
        /**
         * Set Object
         * @param obj Object to set  
         */
        public void setCloneOjb(Object obj){
            aObj = obj;
        }
        
        /**
         * Get Object
         * @return Object
         */
        public Object getCloneOjb(){
           return aObj; 
        }
    }写了一个 小程序 楼主参考一下
      

  6.   

    没必要这么复杂的
    public class Address
    {
      public String country;
      public String city;
      public String section;
      //此处override基类的方法
      public Object clone()
      {
        Address addr=new Address();
        addr.country=this.country;
        addr.city=this.city;
        addr.section=this.section;
        return addr;
      }
      public String toString()
      {
        return country+","+city+","+section;
      }
    }public class Test
    {
      public static void main(String[] args)
      {
        ArrayList list=new ArrayList();
        Address ad=new Address();
        ad.country="中国";
        ad.city="北京";
        ad.section="010";
        list.add(ad.clone);    ad.country="中国";
        ad.city="上海";
        ad.section="021";
        list.add(ad.clone);
        for(int i=0;i<list.size();i++) 
        {
          System.out.print(list.get(i).toString());
        }
      }
    }