新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > JAVA > 正文:Java关于克隆与“冷藏”和“解冻”方法

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

新客网 XKER.COM 2007-08-28 来源: dxaw 收藏本文

 

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

}
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐