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());
}
}