一个对象(如Obj)初始化后设置不同的赋值过程,目前想记录每次对象重新赋值之前的
记录,如何实现?以下arrayDataList.add(调用其他方法返回对象);中返回对象就是Obj,
arrayDataObj 中存储的就是每次Obj的信息,调试过程中发现arrayDataObj 的结果为最后一次
调用返回的Obj的内容,而且所有的都一样,因为指向了同一个引用ID,帮忙看看clone方法
是否正确?如何修改,才能使拷贝生效,而且是对象的拷贝不是引用的拷贝,谢谢!
===========================================
ArrayList arrayDataList = new ArrayList();
for (int j = 0; j < arrayValues.length; j++) {
arrayDataList.add(调用其他方法返回对象);
CloneHandler testCone = new CloneHandler();
testCone.setCloneOjb(arrayDataList.get(0));
CloneHandler resultCone = (CloneHandler) testCone.clone();
arrayDataObj[j] = resultCone.getCloneOjb();
paraType_ = arrayDataList.get(0).getClass();
}===========================================
重写的clone方法:
public class CloneHandler implements Cloneable{ private Object aObj;
/**
* Clone Object According to input Object
* @return Object result of clone
* @throws CloneNotSupportedException
*/
public Object clone(){ CloneHandler o = null;
try{
o = (CloneHandler)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
/**
* Set Object
* @param obj Object to set
*/
public void setCloneOjb(Object obj){
aObj = obj;
}
/**
* Get Object
* @return Object
*/
public Object getCloneOjb(){
return aObj;
}
===========================================
记录,如何实现?以下arrayDataList.add(调用其他方法返回对象);中返回对象就是Obj,
arrayDataObj 中存储的就是每次Obj的信息,调试过程中发现arrayDataObj 的结果为最后一次
调用返回的Obj的内容,而且所有的都一样,因为指向了同一个引用ID,帮忙看看clone方法
是否正确?如何修改,才能使拷贝生效,而且是对象的拷贝不是引用的拷贝,谢谢!
===========================================
ArrayList arrayDataList = new ArrayList();
for (int j = 0; j < arrayValues.length; j++) {
arrayDataList.add(调用其他方法返回对象);
CloneHandler testCone = new CloneHandler();
testCone.setCloneOjb(arrayDataList.get(0));
CloneHandler resultCone = (CloneHandler) testCone.clone();
arrayDataObj[j] = resultCone.getCloneOjb();
paraType_ = arrayDataList.get(0).getClass();
}===========================================
重写的clone方法:
public class CloneHandler implements Cloneable{ private Object aObj;
/**
* Clone Object According to input Object
* @return Object result of clone
* @throws CloneNotSupportedException
*/
public Object clone(){ CloneHandler o = null;
try{
o = (CloneHandler)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
/**
* Set Object
* @param obj Object to set
*/
public void setCloneOjb(Object obj){
aObj = obj;
}
/**
* Get Object
* @return Object
*/
public Object getCloneOjb(){
return aObj;
}
===========================================
解决方案 »
- 怎么样才能做出好看的jprogressbar??? 有支持外观的方法啊
- 找出不在"<>" 或 "</>"之内的所有空格,正则表达式该怎么写?
- swt-designer里面的DragSource,DropTarget能否拖拽按钮?
- 换工作真的这么难吗?
- 请教高手,如何优化java程序的启动速度
- 请问大家,当窗口中有大量的菜单项时怎么进行监听啊?
- 不使用括号,如何算出7个4得出24来??
- 上学时没好好学习,现在毕业两年半了,还不会编程,其间转过行,又转回来了,各位兄弟姐妹给指条路啊
- 怎么实现单击一个按钮时关闭本窗体呀?答完就结帐.
- 初学GUI,遇到的一个问题(附例程)
- 把Java应用发布到没有安装jre的机器上时,所需要的文件。
- 新手初学java,关于汉字显示问题,高手赐教(小妹谢谢各位大侠)!
public Object clone()
{
try
{
yourClass cloned=(yourClass)super.clone();
//添加需要自己clone的东西;
return cloned;
}catch(ClonedNotSupportedException e)
{
return null;
}
}然后你在返回此类的对象的时候候,即可调用clone方法获得一个拷贝,而不是一个引用的拷贝
如:对象Obj = [中国,北京,010]
第二次对象赋值Obj = [中国,上海,021]
第三次对象赋值Obj = [中国,西安,029]Obj每次赋值过程都是针对同一个内存地址进行引用,
想把每次赋值过程产生一个拷贝对象,存放起来。
在调用的类中添加clone()和单独定义的clone()有什么区别呢?
“ //添加需要自己clone的东西;”不是需要通过方法来产生的吗?
没看明白~
调用其他方法返回对象 ,这个对象就要实现clone接口
public static void main(String[] args) {
List dataList = new ArrayList();
Address add = new Address("中国","上海","021");
for(int i = 0 ;i < 10;i++ ) {
CloneHandler testCone = new CloneHandler();
testCone.setCloneOjb(add);
CloneHandler resultCone = (CloneHandler)testCone.clone();
dataList.add(resultCone.getCloneOjb());
add.setAreaNum(add.getAreaNum()+i);
}
Iterator it = dataList.iterator() ;
while(it.hasNext()) {
System.out.println( ((Address)it.next()).toString());
}
}
}class Address implements Cloneable {
private String country; private String city; private String areaNum;
public Address() {
super();
}
public Address(String country, String city, String areaNum) {
super();
this.country = country;
this.city = city;
this.areaNum = areaNum;
}
public Object clone() {
try {
return super.clone();
} catch (Exception e) {
return null;
}
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public String getCountry() {
return country;
} public void setCountry(String country) {
this.country = country;
} public String getAreaNum() {
return areaNum;
} public void setAreaNum(String areaNum) {
this.areaNum = areaNum;
}
public String toString() {
return "["+this.country+" , "+this.city+" , "+this.areaNum+"]" ;
}
}////////////////////////////////////
class CloneHandler implements Cloneable{ private Object aObj;
/**
* Clone Object According to input Object
* @return Object result of clone
* @throws CloneNotSupportedException
*/
public Object clone(){ CloneHandler o = null;
try{
o = (CloneHandler)super.clone();
o.setCloneOjb(((Address)this.aObj).clone()); //拷贝属性
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
/**
* Set Object
* @param obj Object to set
*/
public void setCloneOjb(Object obj){
aObj = obj;
}
/**
* Get Object
* @return Object
*/
public Object getCloneOjb(){
return aObj;
}
}写了一个 小程序 楼主参考一下
public class Address
{
public String country;
public String city;
public String section;
//此处override基类的方法
public Object clone()
{
Address addr=new Address();
addr.country=this.country;
addr.city=this.city;
addr.section=this.section;
return addr;
}
public String toString()
{
return country+","+city+","+section;
}
}public class Test
{
public static void main(String[] args)
{
ArrayList list=new ArrayList();
Address ad=new Address();
ad.country="中国";
ad.city="北京";
ad.section="010";
list.add(ad.clone); ad.country="中国";
ad.city="上海";
ad.section="021";
list.add(ad.clone);
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i).toString());
}
}
}