如何克隆在Android的对象? [英] how to clone an object in android?

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

问题描述

对不起,一个新手的问​​题,但什么是复制的最佳方式/克隆对象在Java / Android的?

Sorry for a novice question, but what would be the best way to copy/clone an object in java/android?

rlBodyDataObj rlbo = bdoTable.get(name);

现在code,从一个Hashtable分配一个对象,但我需要得到它的一个克隆,使我能够多次使用。

right now code assigns an object from a hashTable, yet I need to get a clone of it, so that I'd be able to use it multiple times.

谢谢!

推荐答案

请确保您的DataObj类实现了Cloneable,并添加下面的方法

Make sure that your DataObj class implements Cloneable and add the following method

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

那么你应该能够调用(DataObj)rlBodyDataObj.clone();得到一个干净的副本(注意投)。

Then you should be able to call (DataObj)rlBodyDataObj.clone(); to get a clean copy (note the cast).

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

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