1、最简单的办法:
a=b;2、或者再把b clone后赋给a,3、或者,自己实现一个方法实现属性拷贝,4、如果是bean,可以使用jakarta提供的BeanUtil进行属性复制。

解决方案 »

  1.   

    1、2两种方法都会使a的地址不再指向原来的位置,A也不是bean。只有3可行,但是实现起来过于复杂。
      

  2.   

    那可能就没有什么办法了,不过你可以通过reflection来实现两个类之间的属性复制,
    这样就不用一个个的写了。
      

  3.   

    是单一类还是需要对所有的类?你需要做deep clone,实现Cloneable,实现public Object clone() , 参考http://www.churchillobjects.com/c/11091d.html或参考
    How can I do a deep clone of an object? 
    http://www.jguru.com/faq/view.jsp?EID=20435
    YourClasa a = ..;
    YourClass b = (YourClass)a.Clone();
    try
    {
      a.setProperty(xxx);
    }
    catch (SomeException ex)
    {
      a = (YourClass)b.Clone();
    }
      

  4.   

    使用引用就可以了,a=b,这个时候a和b指向同一个对象,如果还想要一个副本,那只有
    把b  clone一份了,然后把这个副本付给a,a=b.clone()。特别强调一点,java中a,b是引用变量,仅仅是指向某一个对象而已,本身并不是什么对象。
      

  5.   

    class DepthReading implements Cloneable {
      private double depth;
      public DepthReading(double depth) { 
        this.depth = depth;
      }
      public Object clone() {
        Object o = null;
        try {
          o = super.clone();
        } catch(CloneNotSupportedException e) {
          e.printStackTrace(System.err);
        }
        return o;
      }
    }class TemperatureReading implements Cloneable {
      private long time;
      private double temperature;
      public TemperatureReading(double temperature) {
        time = System.currentTimeMillis();
        this.temperature = temperature;
      }
      public Object clone() {
        Object o = null;
        try {
          o = super.clone();
        } catch(CloneNotSupportedException e) {
          e.printStackTrace(System.err);
        }
        return o;
      }
    }class OceanReading implements Cloneable {
      private DepthReading depth;
      private TemperatureReading temperature;
      public OceanReading(double tdata, double ddata){
        temperature = new TemperatureReading(tdata);
        depth = new DepthReading(ddata);
      }
      public Object clone() {
        OceanReading o = null;
        try {
          o = (OceanReading)super.clone();
        } catch(CloneNotSupportedException e) {
          e.printStackTrace(System.err);
        }
        // Must clone references:
        o.depth = (DepthReading)o.depth.clone();
        o.temperature = (TemperatureReading)o.temperature.clone();
        return o; // Upcasts back to Object
      }
    }public class DeepCopy {
      public static void main(String[] args) {
        OceanReading reading = 
          new OceanReading(33.9, 100.5);
        // Now clone it:
        OceanReading r = 
          (OceanReading)reading.clone();
      }
    }=============================================
    这是thinking in java上的深层COPY的代码,要Clone得进行下面的步骤:1.要clone的类得实现Cloneable接口2.override colne这个方法并让它成为public3.在此方法中调用super.clone();
      

  6.   

    clone就行了!b是a的clone,然后再a是b的clone
      

  7.   

    你怎么把a给b的就怎么把b给a呗
      

  8.   

    不知道我的理解是否正确,如果A类实例化的对象a只存在这一层关系的引用,大可以如上面大家所说的做,即a=b即可,
    如果对象a又由其它引用了,如request.setAttribute("c",a);
    那么在做a=b的后重新做request.setAttribute("c",a);的引用。否则就需要为a的属性赋值了,而不能使用a=b的方式。
      

  9.   

    JDK中有具体的内容,关于复制和科隆对象的
      

  10.   

    你的意思是撤消/重做(undo/redo)功能么?简单的做法是,生成一个对象,把当前对象的数据放在里面,你需要撤销时,把那个对象的数据拷贝回当前对象里的变量