Cloneable --shadow clone --deep clone

java 随笔

什么场景应实现Cloneable接口 --共享、缓存数据。

java的Cloneable接口是一个对象(堆)的拷贝技术,类似于serializable接口。

java中的全部对象和变量都存放在堆栈中。Cloneable接口就是在堆栈中新建立一个与被copy对象在堆栈中一样的地址块。


package zl.a.clone;

/**
 * java对象的copy功能
 * <p>
 * shadow clone
 */
public class ClientContext implements Cloneable{

private String protocol;
private String host;
private int port;
private Object client;

// 从新定义构造
public ClientContext(Object client,String protocol,String host,int port){
this.protocol = protocol;
this.port = port;
this.host = host;
this.client = client;
}
public ClientContext(String protocol,String host,int port){
this.protocol = protocol;
this.port = port;
this.host = host;
}
@Override
public Object clone() {
ClientContext clientCtx = null;
try {
clientCtx = (ClientContext) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}

return clientCtx;
}

public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public Object getClient() {
return client;
}
public void setClient(Object client) {
this.client = client;
}
}



import static org.junit.Assert.*;
import static zl.a.impor.StringUtils.isEmpty;


import org.junit.Test;


/**
 * 显示静态导入
 */
public class StringUtilsTest {


@Test
public void testIsEmpty() {
String tem = "123";
assertTrue(isEmpty(tem));
}

}

package zl.a.clone;
import static org.junit.Assert.*;
import java.util.Date;
import org.junit.Test;
import zl.a.impor.StringUtils;
public class ClientContextTest {
@Test
public void test() {

DeepCloneObj deepObj = new DeepCloneObj();
deepObj.setName("007");
deepObj.setPwd("123456789");
ClientContext clientCtx = new ClientContext(deepObj, "http", "127.0.0.1", 8080);

ClientContext clientCtxCopy = (ClientContext)clientCtx.clone();

assertEquals("http", clientCtxCopy.getProtocol());
assertEquals("127.0.0.1", clientCtxCopy.getHost());
assertEquals(8080,clientCtxCopy.getPort());

DeepCloneObj temp = (DeepCloneObj)clientCtxCopy.getClient();
assertFalse(clientCtxCopy.getClient() == null);
assertEquals("007", temp.getName());

deepObj.setName("008");
assertEquals("008", temp.getName());
}
}


--------------------------------------以上文章仅说明了 shadow clone---------------------------------deepClone 下文--------------------------------------------------------------------

package zl.a.clone;


public class DeepCloneObj  implements Cloneable{

private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}

@Override
public Object clone() {
DeepCloneObj deepObj = null;
try {
deepObj = (DeepCloneObj) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return deepObj;
}
}

package zl.a.clone;


import static org.junit.Assert.*;


import java.util.Date;


import org.junit.Test;


import zl.a.impor.StringUtils;


public class ClientContextTest {


@Test
public void test() {

DeepCloneObj deepObj = new DeepCloneObj();
deepObj.setName("007");
deepObj.setPwd("123456789");
ClientContext clientCtx = new ClientContext(deepObj, "http", "127.0.0.1", 8080);

ClientContext clientCtxCopy = (ClientContext)clientCtx.clone();

assertEquals("http", clientCtxCopy.getProtocol());
assertEquals("127.0.0.1", clientCtxCopy.getHost());
assertEquals(8080,clientCtxCopy.getPort());

DeepCloneObj temp_1 = (DeepCloneObj)clientCtxCopy.getClient();
DeepCloneObj temp = (DeepCloneObj) temp_1.clone();
assertFalse(clientCtxCopy.getClient() == null);
assertEquals("007", temp.getName());

temp_1.setName("008");
assertEquals("008", temp.getName());


}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值