java 中的 拷贝分为浅拷贝 和 深拷贝

浅拷贝需要实现Cloneable接口,深拷贝需要实现Serializable接口。

public class Square implements Cloneable, Serializable
{
private Point location = new Point(0, 0); private float sideLength = 1F; @Override
public Object clone()
{
Square tmp = null;
try
{
tmp = (Square) super.clone();
}
catch (CloneNotSupportedException cnse)
{
cnse.printStackTrace();
}
finally
{
return tmp;
}
} /**
* 深克隆方法
* @return
*/
public Object deepClone()
throws IOException, OptionalDataException, ClassNotFoundException
{
// 首先将对象写到流里
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(this); // 然后将对象从流里读出来
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi); return (oi.readObject());
} /**
* @return the location
*/
public Point getLocation()
{
return location;
} /**
* @param location the location to set
*/
public void setLocation(Point location)
{
this.location = location;
} /**
* @return the sideLength
*/
public float getSideLength()
{
return sideLength;
} /**
* @param sideLength the sideLength to set
*/
public void setSideLength(float sideLength)
{
this.sideLength = sideLength;
} }

最新文章

  1. HahsRouter hash 路由
  2. 基于Jquery的页面过渡效果(原创)
  3. Extjs swfUpload 多图片上传
  4. linux-查看系统是32位还是64位
  5. Flex Builder读书笔记(一)
  6. Fedora下载地址
  7. [转]ef获取某个表中的部分字段值
  8. 段(SEGMENT)
  9. protocol(协议)
  10. 通过openssh远程登录时的延迟问题解决
  11. Linux解析内核源代码——传输控制块诞生
  12. ATMEGA16 IOport相关汇总
  13. LayUI之table数据表格获取行、行高亮等相关操作
  14. android 加载图片oom若干方案小结
  15. 【freeradius2.x】 安装和学习
  16. 学习iOS最权威的网站
  17. dubbo和rabbitmq对比
  18. Spring MVC的Post请求参数中文乱码的原因&处理
  19. DB2与Sybase/Oracle/Informix的比较
  20. VS2010 配置PCL1.6.0AII in one 无法启动程序ALL_BUILD

热门文章

  1. java对称加密(AES)
  2. c 语言练习__去掉多余的空白字符_修正
  3. Android新版本SDK打开旧版本项目报错解决
  4. NDK(20)JNI的5大性能缺陷及优化技巧
  5. [Codeforces137A]Postcards and photos(模拟)
  6. poj 3273 Monthly Expense(贪心+二分)
  7. Qt之进程间通信(Windows消息)
  8. SharePoint的安装配置
  9. 在Windows下利用php自带的mail函数发邮件
  10. poj 1392 Ouroboros Snake