Java里关于克隆与“冷藏”和“解冻”方法[00原创]
来源:互联网 发布:mac word 文件恢复 编辑:程序博客网 时间:2024/04/27 20:31
import java.awt.Point;
import java.io.IOException;
import com.sun.corba.se.impl.io.OptionalDataException;
/**
* 克隆测试<br>
* 以方形类为例,比较了深克隆(deep clone)与浅克隆(shallow clone)的异同
*
* @see #clone()
* @author 88250
* @version 1.0.0, 2007-8-26
*/
public class CloneTester
{
private Square square = new Square();
private Square cpySquare = null;
/**
* 浅克隆操作
*/
public void shallowClone()
{
square.setSideLength(2);
square.setLocation(new Point(2, 5));
// 浅克隆
cpySquare = (Square) square.clone();
}
/**
* 深克隆操作
*/
public void deepClone()
{
square.setSideLength(3);
square.setLocation(new Point(1, 3));
// 深克隆
try
{
cpySquare = (Square) square.deepClone();
}
catch (OptionalDataException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
/**
* 克隆结果输出
*/
public void cloneDisplay()
{
System.out.println("原始方形长度:" + square.getSideLength());
System.out.println("克隆方形长度:" + cpySquare.getSideLength());
System.out.println("原始方形==克隆方形?" + (square == cpySquare));
System.out.println("原始方形的位置==克隆方形的位置?"
+ (square.getLocation() == cpySquare.getLocation()));
}
public static void main(String[] args)
{
CloneTester sm = new CloneTester();
sm.shallowClone();
sm.cloneDisplay();
sm.deepClone();
sm.cloneDisplay();
}
}
import java.io.IOException;
import com.sun.corba.se.impl.io.OptionalDataException;
/**
* 克隆测试<br>
* 以方形类为例,比较了深克隆(deep clone)与浅克隆(shallow clone)的异同
*
* @see #clone()
* @author 88250
* @version 1.0.0, 2007-8-26
*/
public class CloneTester
{
private Square square = new Square();
private Square cpySquare = null;
/**
* 浅克隆操作
*/
public void shallowClone()
{
square.setSideLength(2);
square.setLocation(new Point(2, 5));
// 浅克隆
cpySquare = (Square) square.clone();
}
/**
* 深克隆操作
*/
public void deepClone()
{
square.setSideLength(3);
square.setLocation(new Point(1, 3));
// 深克隆
try
{
cpySquare = (Square) square.deepClone();
}
catch (OptionalDataException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
/**
* 克隆结果输出
*/
public void cloneDisplay()
{
System.out.println("原始方形长度:" + square.getSideLength());
System.out.println("克隆方形长度:" + cpySquare.getSideLength());
System.out.println("原始方形==克隆方形?" + (square == cpySquare));
System.out.println("原始方形的位置==克隆方形的位置?"
+ (square.getLocation() == cpySquare.getLocation()));
}
public static void main(String[] args)
{
CloneTester sm = new CloneTester();
sm.shallowClone();
sm.cloneDisplay();
sm.deepClone();
sm.cloneDisplay();
}
}
import java.awt.Point;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import com.sun.corba.se.impl.io.OptionalDataException;
/**
* 正方形
*
* @author 88250
* @version 1.0.0, 2007-8-26
*/
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;
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import com.sun.corba.se.impl.io.OptionalDataException;
/**
* 正方形
*
* @author 88250
* @version 1.0.0, 2007-8-26
*/
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;
}
}
- Java里关于克隆与“冷藏”和“解冻”方法[00原创]
- 《Java与模式》: 21 专题:javaBean的“冷藏”和“解冻”
- JavaBean的冷藏与解冻
- 冷藏 解冻 深拷贝deepclone
- JAVA里的深克隆与浅克隆实现
- 关于java的深克隆与浅克隆
- 关于Java里的hashcode和equal方法
- 关于java克隆
- 关于java中的克隆
- 关于object类的clone方法浅克隆与深度克隆
- JAVA 通用克隆方法
- java深度克隆方法
- 关于Java深度克隆和浅度克隆的简单说明
- [Java] 集合类克隆方法与内部排序
- Java对象克隆方法(浅克隆、深克隆)
- Java对象克隆方法(浅克隆、深克隆)
- Java clone() 浅克隆与深度克隆
- java 深克隆与浅克隆
- ResultSet can not re-read row data
- 我理解的windows异常处理
- 无商不奸啊!
- 社会必将淘汰的8种人[转帖]
- How to log application API calls using import module addresses
- Java里关于克隆与“冷藏”和“解冻”方法[00原创]
- 原型(Prototype)模式的Java实现[00原创]
- 重建ORACLE数据库索引
- ORACLE分页查询SQL语法
- ORACLE查询或删除时指定使用索引的写法
- 我很好的一个同学,经商的
- 快速获取ORACLE表字段的清单
- display:inline
- 夜半醒来