0

Cloneable インターフェイスを使用して複雑なオブジェクトを複製しようとしています。

Resource という名前のスーパー オブジェクトがあり、次の 2 つのフィールドがあります。

   public class Resource implements Cloneable{
        protected String name;
        protected boolean isOnline;

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
   }

また、Cursor、HashMap、DBHelper などの複雑なフィールドを持つ Resource を拡張するこのクラス CollectionResource など:

 public class CollectionResource extends Resource{
    DbHelper myDb;
    Cursor currentCursor;
    Cursor tmpCursor;
    HashMap<String, FieldType> fieldsTypes=null;
    private String currenViewName;
    private String currentDateField;    
    boolean datesetMode=false;
    public boolean singleRowMode=false;
    private int lastId=-2;
    int retainCount=0;
    public double lasEditTime=0;

    @Override
    public Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
}

別のインスタンスで作業する必要があるため、CollectionResource のインスタンスを複製する必要があるため、単純にこれを実行してみました。

try {
    this.collection = (CollectionResource) collectionToClone.clone();
} catch (CloneNotSupportedException e) {
    e.printStackTrace();
}

それはうまくいくようです...しかし、私はResourceCollection内で複雑なオブジェクトを使用していて、ソースコードにディープクローンを実装していないため、理由がわかりません...

ディープ クローニングなしで機能するのはなぜですか? DBHelper、HashMap、Cursor などの複雑なオブジェクトがディープ クローニングなしでクローンされるのはなぜですか? 私はそれを適切に行っておらず、近い将来エラーが見つかると思います。

ありがとう

4

0 に答える 0