在Java中深度克隆类的对象 [英] Deep cloning an object of a class in java

查看:611
本文介绍了在Java中深度克隆类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主类中有一个Game.java类的对象游戏。我需要创建游戏对象的副本(game_copy),以便在对game_copy的面板进行更改时,不会更改游戏的面板。我将这个game_copy对象设为:

I have an object game of my Game.java class in my main class. I need to create a copy (game_copy) of game object such that when I make changes to the board of game_copy, the board of game does not get changed. I make this game_copy object as:

Game game_copy = new Game() ;
game_copy = game.clone() ;

但是,当我更改game_copy的控制板时,游戏控制板仍会按照原样进行更改分享参考。我该如何解决这个问题?
这是我正在使用的Game和rplayer类:

But still when I make changes to the board of game_copy, the board of game gets changed as they still share a reference. How do I sort out this problem? Here are my Game and rplayer classes that I am using:

class Game implements Cloneable{
     int n;
     ArrayList<ArrayList<String>> board;
     rplayer [] players;
     int a ;

    public Game(int n){
      this.n=n;
      players = new rplayer[2] ;
      players[0]=new rplayer(this.a);
      players[1]=new rplayer(this.a);
      board= new ArrayList<ArrayList<String>>();
       for(int p= 0 ; p < n*n ; p++){
        ArrayList<String> r = new ArrayList<String>() ;
        board.add(r) ;
       }
     }

   public Game clone(){ 
    Game gm ;
    try { 
        gm =  (Game) super.clone(); 
    } 
    catch (CloneNotSupportedException e) { 
        System.out.println (" Cloning not allowed. " );
         return null; 
    }
    return gm
 }
}

class rplayer{
    int a;
    public rplayer(int a){
      this.a=a;
    }
}

这是我在尝试使用.clone之前尝试过的方法()方法,但是制作副本构造函数的想法也不起作用。

This is what I tried before trying to use .clone() method, but the idea of making a copy constructor didn't work either. Both the objects were always related.

public Game(Game g){
this.n=g.n;
this.players = new rplayer[2] ;
rplayer[] temp_players = new rplayer[2] ;
temp_players = g.players;
this.players = temp_players ;
this.board= new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> temp_board = new ArrayList<ArrayList<String>>() ;
    for(int p= 0 ; p < n*n ; p++){
        ArrayList<String> r = new ArrayList<String>() ;
        board.add(r) ;
        temp_board.add(r) ;
    }
    temp_board = g.board ;
    board= temp_board;
}


推荐答案

我发现可以做的事情通过使用 Serializable 接口将对象标记为复杂对象的深层副本,然后在内存中序列化该对象。由于您的游戏对象引用了另一个对象,并且该对象也引用了其他对象,因此最好通过序列化来克隆该对象。这将确保您获得完全不同的对象。由于对象变得越来越复杂,Java克隆将无法达到目的。

What I have found to make deep copy of a complex object by doing mark the object with Serializable interface and then serialize the object in memory. As your Game object has reference to another object and that object also has the reference to others it would be better to clone the object through serialization. This will ensure you that you will get absolutely different object. Java clone will not serve the purpose as the objects are getting complicated.

下面是示例代码,说明如何序列化内存中的对象。

here is the sample code how to serialize object in memory.

class Game implements Serializable {

........
........
........

public Game makeClone() throws IOException, ClassNotFoundException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(outputStream);
    out.writeObject(this);

    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    ObjectInputStream in = new ObjectInputStream(inputStream);
    Game copied = (Game) in.readObject();
    return copied;
}

}

这篇关于在Java中深度克隆类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆