服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java关于克隆与“冷藏”和“解冻”方法

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.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;
    }

}

扫描关注微信公众号