直接implements Cloneable然后clone();

解决方案 »

  1.   

    我把原来一个C#的程序想用java改写,中间要复制一个image对象,C#中类似Image c = (Image)b.clone()就完成了
    现在在java中,我试着写了一个
    abstract class myImage extends Image implements Cloneable{
        public Object clone() {
            Object o = null;
            try {
                o = super.clone();
            }
            catch(CloneNotSupportedException cnse) {
                cnse.printStackTrace();
            }
            return o;
        }
    }
    但createImage方法返回的是Image对象,myImage类就用不上了。
    sigh~,谁能帮一把?
      

  2.   

    你要明白这个问题还是好好去研究一下 clone
      

  3.   

    我稍微看了一点,分什么shallow clone和deep clone,似乎就是考虑到被克隆对象里包含的对象问题吧
    我现在只是简单得想把一个Image对象复制一下,应属于shallow clone吧,不知什么方法可以?
      

  4.   

    o = super.clone();
    应该就是返回自身把。
      

  5.   

    如果你是shallow Clone那么,就return super.clone();
    就可以。如果要是深度Clone的话,那么就要自己做了
    myImage o = (myImage)  super.clone();
    然后就自己做可以了:)
      

  6.   

    深度克隆和浅克隆的离职,我修改并测试了,利用了think in java 中的例子修改的,呵呵
    class DepthReading implements Cloneable {
      private double depth;
      public DepthReading(double depth) { 
        this.depth = depth;
      }
      public void setDepth(double newDepth){
      this.depth = newDepth;
      }
      public double getDepth(){
      return this.depth;
      }
      public Object clone() {
        Object o = null;
        try {
          o = super.clone();
        } catch (CloneNotSupportedException e) {
          e.printStackTrace();
        }
        return o;
      }
    }
    //-----------------------------------------------------------------------------------------------------
    class TemperatureReading implements Cloneable {
      private long time;
      private double temperature;
      public TemperatureReading(double temperature) {
        time = System.currentTimeMillis();
        this.temperature = temperature;
      }
     public void setTemperature(double newTemperature){
      this.temperature = newTemperature;
      }
      public double getTemperature(){
      return this.temperature;
      }
      public Object clone() {
        Object o = null;
        try {
          o = super.clone();
        } catch (CloneNotSupportedException e) {
          e.printStackTrace();
        }
        return o;
      }
    }
    //-----------------------------------------------------------------------------------------------------
    class OceanReading implements Cloneable {
      private DepthReading depth;
      private TemperatureReading temperature;
      private java.util.GregorianCalendar gc;
      private String str;
      private Integer integer;
      private Boolean bool;
      public OceanReading(double tdata, double ddata){
        temperature = new TemperatureReading(tdata);
        depth = new DepthReading(ddata);
      }
     
     public void setGC(java.util.GregorianCalendar newGC){
     this.gc = newGC;
     }
     public java.util.GregorianCalendar getGC(){
     return this.gc;
     }
     public void setInteger(Integer newInt){
    this.integer = newInt;
     }
     public Integer getInteger(){
     return this.integer;
     }
     public DepthReading getDepthReading (){
     return depth;
     }
     public TemperatureReading getTemperatureReading(){
     return temperature;
     }
      public Object clone() {
        OceanReading o = null;
        try {
          o = (OceanReading)super.clone();
        } catch (CloneNotSupportedException e) {
          e.printStackTrace();
        }
        // Must clone handles:
        //o.depth = (DepthReading)o.depth.clone();
       // o.temperature = (TemperatureReading)o.temperature.clone();
        o.depth = this.getDepthReading();
        o.temperature = this.getTemperatureReading();    return o; // Upcasts back to Object
      }
    }
    //-----------------------------------------------------------------------------------------------------
    public class DeepCopy {
      public static void main(String[] args) {
        OceanReading reading = 
          new OceanReading(33.9, 100.5);
    reading.setInteger(new Integer(5));
    reading.setGC(new java.util.GregorianCalendar());
        // Now clone it:
    System.out.println(reading.getTemperatureReading().getTemperature());
    System.out.println(reading.getInteger().toString());
    System.out.println(reading.getGC().get(java.util.GregorianCalendar.YEAR));
        OceanReading r = 
          (OceanReading)reading.clone();
    r.setInteger(new Integer(8));
    r.getTemperatureReading().setTemperature(1.2);
    //r.setGC(new java.util.GregorianCalendar(2008,8,8));
    r.setGC(reading.getGC());
    r.getGC().set(java.util.GregorianCalendar.YEAR,2008);
    System.out.println(r.getTemperatureReading().getTemperature());
    System.out.println(r.getInteger().toString());
    System.out.println(r.getGC().get(java.util.GregorianCalendar.YEAR));
    System.out.println(reading.getTemperatureReading().getTemperature());
    System.out.println(reading.getInteger().toString());
    System.out.println(reading.getGC().get(java.util.GregorianCalendar.YEAR));  }
    }
      

  7.   

    可以用myimage实现clone,可参数只能接受为image类型,怎么办?